-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
46 lines (36 loc) · 764 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
39
40
41
42
43
44
45
46
'use strict';
var onetime = require('onetime');
var setImmediateShim = require('set-immediate-shim');
module.exports = function (arr, next, cb) {
var failed = false;
var count = 0;
cb = cb || function () {};
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (typeof next !== 'function') {
throw new TypeError('Second argument must be a function');
}
var len = arr.length;
if (!len) {
cb();
return;
}
function callback(err) {
if (failed) {
return;
}
if (err !== undefined && err !== null) {
failed = true;
cb(err);
return;
}
if (++count === len) {
cb();
return;
}
}
for (var i = 0; i < len; i++) {
setImmediateShim(next, arr[i], i, onetime(callback, true));
}
};