aboutsummaryrefslogtreecommitdiff
path: root/lib/promises.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/promises.js')
-rw-r--r--lib/promises.js71
1 files changed, 63 insertions, 8 deletions
diff --git a/lib/promises.js b/lib/promises.js
index 0da48bb..3620c96 100644
--- a/lib/promises.js
+++ b/lib/promises.js
@@ -1,10 +1,9 @@
var _ = require("underscore");
-var bluebird = require("bluebird/js/release/promise")();
exports.defer = defer;
-exports.Promise = bluebird.Promise;
-exports.resolve = bluebird.resolve;
-exports.reject = bluebird.reject;
+exports.Promise = Promise;
+exports.resolve = Promise.resolve.bind(Promise);
+exports.reject = Promise.reject.bind(Promise);
exports.forEachSeries = function(array, func) {
var arrayIndex = 0;
@@ -31,7 +30,7 @@ var props = exports.props = function(obj) {
var keys = Object.keys(obj);
var values = Object.values(obj);
- var result = bluebird.all(values).then(function(resolvedValues) {
+ var result = Promise.all(values).then(function(resolvedValues) {
var resolvedObj = {};
for (var index = 0; index < keys.length; index++) {
resolvedObj[keys[index]] = resolvedValues[index];
@@ -58,16 +57,16 @@ function addAlsoToPromise(promise) {
exports.try = function(func) {
try {
- return bluebird.resolve(func());
+ return Promise.resolve(func());
} catch (error) {
- return bluebird.reject(error);
+ return Promise.reject(error);
}
};
function defer() {
var resolve;
var reject;
- var promise = new bluebird.Promise(function(resolveArg, rejectArg) {
+ var promise = new Promise(function(resolveArg, rejectArg) {
resolve = resolveArg;
reject = rejectArg;
});
@@ -78,3 +77,59 @@ function defer() {
promise: promise
};
}
+
+function ExternalPromise(executor) {
+ var promise = new Promise(executor);
+ promise.__proto__ = ExternalPromise.prototype;
+ return promise;
+}
+
+ExternalPromise.__proto__ = Promise;
+ExternalPromise.prototype.__proto__ = Promise.prototype;
+
+ExternalPromise.prototype.done = function(resolve, reject) {
+ return this.then(resolve, reject).catch(function(error) {
+ if (isNode()) {
+ var stack = error instanceof Error ? error.stack : error;
+ process.stderr.write("Fatal " + stack + "\n");
+ process.exit(2);
+ } else {
+ setTimeout(function() {
+ throw error;
+ }, 0);
+ }
+ });
+};
+
+/**
+ * Previous versions of Mammoth used bluebird instead of native promises. While
+ * internal usage can be replaced easily enough, external users of the library
+ * may have relied on values returned from function calls being a bluebird
+ * promise.
+ *
+ * However, since the return value was only documented as a promise, not as a
+ * bluebird promise, and it's relatively straightforward to wrap the return
+ * value in a bluebird promise if necessary, changing the API to return a native
+ * promise should be relatively safe.
+ *
+ * The only exception is that the docs previously used `.done()`: therefore,
+ * when returning promises from the API to external users, a `done` method is
+ * added.
+ */
+function toExternalPromise(promise) {
+ return ExternalPromise.resolve(promise);
+}
+
+exports.toExternalPromise = toExternalPromise;
+
+/**
+ * Detect whether we're running in node.js. This uses the same logic as in
+ * bluebird for consistency with previous versions of Mammoth.
+ */
+function isNode() {
+ if (typeof process === "undefined") {
+ return false;
+ }
+
+ return {}.toString.call(process).toLowerCase() === "[object process]";
+}