Run promises in sequence
npm i serialp
const timeout=duration=>()=>new Promise(
resolve=>setTimeout(
()=>{
resolve(duration)
},duration
)
);
const promises = [
() => timeout(100),
() => timeout(200)
];
serialp(promises).then(
results=>console.log(results) // [[true,100],[true,200]]
);
serialp(arrayOfPromisesFunctions,observer)
.then((x=>{}))
.catch (x=>{}) // trigered only if observer is an async function
-
x is an array of [success:boolean,result]
-
observer is
- either a function (index,success,result,results) which returns a boolean
- if observer function returns false, promises execution stops,
- observer function can be an async function
- or a boolean
- if true (default), all promises will be executed,
- if false, promises execution will stop at first failure