-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (29 loc) · 820 Bytes
/
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
'use strict'
function PromiseCChain(promiseFactories) {
this.steps = promiseFactories.map(
function(step) {
if (typeof step === 'string')
return require(step)
else if (typeof step === 'function')
return step
else
throw "factory must be a function that returns a promise, or the name of a module to require which returns a function that returns a promise";
}
);
}
PromiseCChain.factory = function (factories) {
const pc = new PromiseCChain(factories);
return function(context, data1) {
if (!context) {
context = {};
}
return pc.steps.reduce(function(promiseCChain, step) {
return promiseCChain.then(function(data) {
if (data === true)
return Promise.resolve(data);
return step(context, data);
});
}, Promise.resolve());
}
}
module.exports = PromiseCChain;