Promise library in 137 chars
Execute in series and wait for result
// [() -> P a, a -> P b, ..., y -> P z] -> P z
P.ser([ ... ])
Execute in parallel and wait for result
// [() -> P (), ...] -> P ()
P.par([ ... ])
const P = require('./P.js') | |
const delay = ms => new Promise(ok => setTimeout(ok, ms)) | |
const log = x => () => console.log(x) | |
P.ser([ | |
() => delay(2000).then(log('1')), | |
() => delay(1500).then(log('2')), | |
() => delay(1000).then(log('3')) | |
]) | |
.then(() => P.par([ | |
() => delay(2000).then(log('3')), | |
() => delay(1500).then(log('2')), | |
() => delay(1000).then(log('1')) | |
])) | |
.then(() => console.log('done')) |
e=exports | |
e.ser=x=>x.reduce((p,q)=>p?p.then(z=>q(z)):q(),0) | |
e.par=x=>new Promise((k,r)=>{N=x.length;x.map(p=>p().then(_=>--N?_:k(),r))}) |