-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapseqasync2.js
37 lines (32 loc) · 1.44 KB
/
mapseqasync2.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
function promisify(func, inputData) {
return new Promise((resolve, reject) => {
// respData after processing the callback when time out is done
func((err, respData) => {
!err ? resolve(respData) : reject(err);
}, inputData)
})
}
/**
* Using promise
* Step 1: we create a starter promise with initial data in its resolver, accumlatorPromise points to this when we start
* Step 2: loop over the funcs array and create promise for each func(using promisify)
* Step 3: once promisify is resolved it will return a resolve promise with the new data from the func call
and this new resolve promise will be the accumlatorPromise which will call promisify again on the next func in the array
*/
function sequence(funcs){
return function(finalCallback, initialData) {
const starterPromise = Promise.resolve(initialData);
const finalPromise = funcs.reduce((accumlatorPromise, currentFn)=>{
return accumlatorPromise.then(data => {
return promisify(currentFn, data);// this will create a new promise and run the executor which will resolve/reject
})
.catch(err => {
return Promise.reject(err);
});
}, starterPromise);
// last resolve/reject promise in the accumulator
finalPromise
.then(data => {finalCallback(undefined, data)})
.catch(err => {finalCallback(err)} )
};
}