Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
promise sequentially
// https://hackernoon.com/functional-javascript-resolving-promises-sequentially-7aac18c4431e
// https://decembersoft.com/posts/promises-in-serial-with-array-reduce/
function delay() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('RESOLVED')
resolve()
}, 1000 * 1)
})
}
const arr = new Array(10).fill(delay)
const resultPromise = arr.reduce(function (prevPromise, currentPromise) {
return prevPromise.then(prevResults => {
return currentPromise().then(curResult => {
return [...prevResults, curResult]
})
})
}, Promise.resolve([]))
resultPromise.then(results => {
console.log(results)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.