-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (47 loc) · 1.25 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
function sync(timeline, callback, errorCallback) {
var isNull = (timeline == null);
var isArray = Array.isArray(timeline);
var isFunction = (typeof timeline === "function");
if (isArray) {
var first = timeline[0];
var next = (timeline.length == 0 ? null : timeline.slice(1));
var nextCallback = sync.bind(this, next, callback, errorCallback);
sync(first, nextCallback, errorCallback);
}
else if (isFunction) {
try {
timeline(callback);
} catch (err) {
if (errorCallback)
errorCallback(err);
else
throw err;
}
}
else if (!isNull) {
var message = "Only arrays and functions may be included in the timeline, didn't expect type \"" + typeof timeline + "\": " + JSON.stringify(timeline);
if (errorCallback)
errorCallback(message);
else
throw new Error(message);
}
else if (callback)
callback();
}
sync.sync = sync;
sync.s = sync.sync;
sync.parallel = function (timeline) {
return function (callback) {
var total = timeline.length;
var current = 0;
var syncCallback = function () {
current++;
if (total == current)
callback();
};
for (var i = 0; i < timeline.length; i++)
timeline[i](syncCallback);
};
};
sync.p = sync.parallel;
module.exports = sync;