forked from Duke-PL-Course/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbonus-waterfall.js
More file actions
58 lines (55 loc) · 1.39 KB
/
bonus-waterfall.js
File metadata and controls
58 lines (55 loc) · 1.39 KB
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
/**
* @param tasks is an array of functions
* @param callback is the final function
*/
function waterfall(tasks, callback) {
var i = 0;
var cb = function(err) {
var newArgs;
if (err) {
callback(err);
} else {
i += 1;
newArgs = Array.prototype.slice.call(arguments);
if (i === tasks.length) {
callback.apply(null, newArgs);
} else {
newArgs.push(cb);
tasks[i].apply(null, newArgs.slice(1));
}
}
};
tasks[i](cb);
}
// example with non-erroring tasks
waterfall([
// the first task will always have the signature function (callback)
function (callback) {
// do stuff
callback(null, 'one', 'two');
},
function (arg1, arg2, callback) {
callback(null, arg1 + '1', arg2 + '2');
}
], function (err, result1, result2) {
// usually it is not idiomatic to return multiple result parameters;
// however, we want to show that it is possible
console.log(err, result1, result2); // > null "one1" "two2"
});
waterfall([
function (callback) {
if (true) {
// uh-oh, I error out
callback("oh nooooez");
} else {
callback(null, 'would otherwise be foo');
}
},
// the second task should never be called
function (foo, callback) {
console.log("I'm never called");
callback(null, foo + 'bar');
}
], function (err, result) {
console.log(err, result); // > "oh nooooez" undefined
});