forked from es128/async-waterfall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (70 loc) · 2.01 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// MIT license (by Elan Shanker).
(function(globals) {
'use strict';
var nextTick = function (fn) {
if (typeof setImmediate === 'function') {
setImmediate(fn);
} else if (typeof process !== 'undefined' && process.nextTick) {
process.nextTick(fn);
} else {
setTimeout(fn, 0);
}
};
var makeIterator = function (tasks) {
var makeCallback = function (index) {
var fn = function () {
if (tasks.length) {
tasks[index].apply(null, arguments);
}
return fn.next();
};
fn.next = function () {
return (index < tasks.length - 1) ? makeCallback(index + 1): null;
};
return fn;
};
return makeCallback(0);
};
var _isArray = Array.isArray || function(maybeArray){
return Object.prototype.toString.call(maybeArray) === '[object Array]';
};
var waterfall = function (tasks, callback) {
callback = callback || function () {};
if (!_isArray(tasks)) {
var err = new Error('First argument to waterfall must be an array of functions');
return callback(err);
}
if (!tasks.length) {
return callback();
}
var wrapIterator = function (iterator) {
return function (err) {
if (err) {
callback.apply(null, arguments);
callback = function () {};
} else {
var args = Array.prototype.slice.call(arguments, 1);
var next = iterator.next();
if (next) {
args.push(wrapIterator(next));
} else {
args.push(callback);
}
nextTick(function () {
iterator.apply(null, args);
});
}
};
};
wrapIterator(makeIterator(tasks))();
};
if (typeof define !== 'undefined' && define.amd) {
define([], function () {
return waterfall;
}); // RequireJS
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = waterfall; // CommonJS
} else {
globals.asyncWaterfall = waterfall; // <script>
}
})(this);