-
Notifications
You must be signed in to change notification settings - Fork 3
/
series.js
41 lines (31 loc) · 1.14 KB
/
series.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
module.exports = function series(work, callback, thisObj) {
var results = new Array(work.length);
thisObj = thisObj || this;
if (work.length === 0) {
return callback.call(thisObj, null, results);
}
var createCallback = function(index) {
var invoked = false;
return function(err, data) {
if (invoked === true) {
throw new Error('callback for async operation at index "' + index + '" invoked after completion. ' + (err ? (err.stack || err) : '(no error)'));
}
invoked = true;
if (err) {
// stop on first error
return callback.call(thisObj, err);
}
results[index] = data;
var next = index + 1;
if (next === work.length) {
// finished
return callback.call(thisObj, null, results);
} else {
// move on to next work item
work[next].call(thisObj, createCallback(next));
}
};
};
// kick off the tasks by invoking first job
work[0].call(thisObj, createCallback(0));
};