-
Notifications
You must be signed in to change notification settings - Fork 0
/
serpa.js
73 lines (65 loc) · 1.48 KB
/
serpa.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
'use strict';
var q = require("q"),
series,
parallel,
preserve,
split,
notPromise = {};
function slice(arr) {
return Array.prototype.slice.call(arr);
}
function when(soFar, element) {
if (element.notPromise === notPromise) {
return element(soFar);
}
return q(soFar).then(element);
}
preserve = function preserve(fn) {
return function (value){
return q(value)
.then(fn)
.then(function (){
return value;
});
};
};
series = function series() {
var f, args;
args = slice(arguments);
f = function executingSeries(soFar) {
return args.reduce(when, soFar);
};
f.notPromise = notPromise;
return f;
};
parallel = function parallel() {
var f, args;
args = slice(arguments);
f = function executingParallel(soFar) {
return q.all(args.map(
function (el) {
return when(soFar, el);
}
));
};
f.notPromise = notPromise;
return f;
};
split = function split() {
var f, args;
args = slice(arguments);
f = function executingSplit(soFar){
if (Array.isArray(soFar)){
return q.all(soFar.map(function (element){
return args.reduce(when, q(element));
}));
} else {
return q.reject("'split' argument is not an Array: " + soFar.toString());
}
};
return f;
};
exports.parallel = parallel;
exports.preserve = preserve;
exports.series = series;
exports.split = split;