-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
38 lines (36 loc) · 942 Bytes
/
index.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
var Promise = require('promise');
var isArray = Array.isArray || require('isArray');
var isPromise = require('is-promise');
module.exports = all;
function all() {
var args;
if (arguments.length === 1 && (isArray(arguments[0]) || isPromise(arguments[0])))
args = arguments[0];
else
args = Array.prototype.slice.call(arguments);
if (isPromise(args)) {
return args.then(all);
} else {
return _all(args);
}
}
function _all(arr) {
return new Promise(function (resolve, reject) {
var remaining = arr.length;
if (remaining === 0) return resolve([]);
var result = new Array(arr.length);
function res(i, val) {
if (isPromise(val)) {
val.then(function (val) { res(i, val); }, reject);
} else {
result[i] = val;
if (--remaining === 0) {
resolve(result);
}
}
}
for (var i = 0; i < arr.length; i++) {
res(i, arr[i]);
}
});
}