-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublechain.js
33 lines (32 loc) · 976 Bytes
/
doublechain.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
function doubleChain(calls, onComplete) {
/* Calls functions from a list of function descriptions, passing each one
an argument that is a function that must be called when the function is
done.
This allows functions to have asynchronous portions but still be called
in order and the completion of all functions to be acted on.
*/
function callNext(j) {
var next, bind, args;
if (j + 1 == calls.length) {
next = onComplete;
} else {
next = callNext.bind(null, j + 1);
}
if (calls[j].hasOwnProperty('bind')) {
bind = calls[j].bind;
} else {
bind = null;
}
if (calls[j].hasOwnProperty('args')) {
args = calls[j].args.concat(next);
} else {
args = [next];
}
calls[j].func.apply(bind, args);
}
if (calls.length === 0) {
onComplete();
} else {
callNext(0);
}
}