Files: d2f2697f296dd39aed6a8b63c6d04a736a7db5b3 / node_modules / multicb / index.js
1011 bytesRaw
1 | module.exports = function(allopts) { |
2 | var n = 0, m = 0, _cb, results = [], _err; |
3 | function o (k, d) { return allopts && allopts[k] !== void 0 ? allopts[k] : d } |
4 | |
5 | return function(cb) { |
6 | if (cb) { |
7 | results.length = m |
8 | |
9 | if(_err) { |
10 | var err = _err; _err = null |
11 | return cb(err) |
12 | } |
13 | if(n == m) { |
14 | if (o('spread')) |
15 | return cb.apply(null, [null].concat(results)) |
16 | else |
17 | return cb(null, results) |
18 | } |
19 | |
20 | _cb = cb |
21 | return |
22 | } |
23 | |
24 | var i = m++ |
25 | return function (err) { |
26 | if (err) { |
27 | if (_err) return |
28 | _err = err |
29 | n = -1 // stop |
30 | if (_cb) _cb(err) |
31 | } else { |
32 | n++ |
33 | if (o('pluck')) |
34 | results[i] = arguments[o('pluck')] |
35 | else |
36 | results[i] = Array.prototype.slice.call(arguments) |
37 | if (n === m && _cb) { |
38 | if (o('spread')) |
39 | _cb.apply(null, [null].concat(results)) |
40 | else |
41 | _cb(null, results) |
42 | } |
43 | } |
44 | } |
45 | } |
46 | } |
47 |
Built with git-ssb-web