index.js
773 Bytes
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
module.exports = function parallel(fns, context, callback) {
if (!callback) {
if (typeof context === 'function') {
callback = context
context = null
} else {
callback = noop
}
}
var pending = fns && fns.length
if (!pending) return callback(null, []);
var finished = false
var results = new Array(pending)
fns.forEach(context ? function (fn, i) {
fn.call(context, maybeDone(i))
} : function (fn, i) {
fn(maybeDone(i))
})
function maybeDone(i) {
return function (err, result) {
if (finished) return;
if (err) {
callback(err, results)
finished = true
return
}
results[i] = result
if (!--pending) callback(null, results);
}
}
}
function noop() {}