-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall.js
51 lines (45 loc) · 1.02 KB
/
all.js
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
module.exports = function(Promise) {
//
// Returns a new promise which resolves/rejects based on an array of given promises
//
// This is more of a polyfill, as this should exist in a compliant environment
//
// @param {promises} the promises to handle
// @return Promise
//
return function(promises) {
return new Promise(function(resolve, reject) {
if (! Array.isArray(promises)) {
resolve([ ]);
return;
}
var values = [ ];
var finished = false;
var remaining = promises.length;
promises.forEach(function(promise, index) {
var then = utils.thenable(promise);
if (! then) {
onResolve(promise);
return;
}
then.call(promise,
function onResolve(value) {
remaining--;
values[index] = value;
checkIfFinished();
},
function onReject(reason) {
finished = true;
reject(reason);
}
);
});
function checkIfFinished() {
if (! finished && ! remaining) {
finished = true;
resolve(values);
}
}
});
};
};