chain up promises (i.e. process them serially)?
1) Bluebird library: Promise.mapSeries(promises)
2) Functional style: arr.reduce((chain, a) => chain.then(() => doSomethingAsync(a))), Promise.resolve())
3) Recursive function:
function recursiveFn(arr) {
if (arr.length === 0) {
return Promise.resolve()
}
const a = arr.shift() // shift off 1st element & return it
return doSomethingAsync(a).then(() => recursiveFn(arr))
}
4) Other way?
if I use Bluebird already, then definitely Bluebird version. (After diskovering Fluture, I think I'm unlikely to bring Bluebird into a project tho)
Обсуждают сегодня