1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
var _ = require("underscore");
exports.defer = defer;
exports.Promise = Promise;
exports.resolve = Promise.resolve.bind(Promise);
exports.reject = Promise.reject.bind(Promise);
exports.forEachSeries = function(array, func) {
var arrayIndex = 0;
var deferred = defer();
var next = function() {
if (arrayIndex < array.length) {
var element = array[arrayIndex];
arrayIndex++;
func(element).then(next, deferred.reject);
} else {
deferred.resolve();
}
};
next();
return deferred.promise;
};
var props = exports.props = function(obj) {
// We rely on .keys() and .values() returning properties in the same order.
var keys = Object.keys(obj);
var values = Object.values(obj);
var result = Promise.all(values).then(function(resolvedValues) {
var resolvedObj = {};
for (var index = 0; index < keys.length; index++) {
resolvedObj[keys[index]] = resolvedValues[index];
}
return resolvedObj;
});
return addAlsoToPromise(result);
};
var alsoMethod = function(func) {
var result = this.then(function(value) {
var returnValue = _.extend({}, value, func(value));
return props(returnValue);
});
return addAlsoToPromise(result);
};
function addAlsoToPromise(promise) {
promise.also = alsoMethod;
return promise;
}
exports.try = function(func) {
try {
return Promise.resolve(func());
} catch (error) {
return Promise.reject(error);
}
};
function defer() {
var resolve;
var reject;
var promise = new Promise(function(resolveArg, rejectArg) {
resolve = resolveArg;
reject = rejectArg;
});
return {
resolve: resolve,
reject: reject,
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]";
}
|