Skip to content

Instantly share code, notes, and snippets.

@thcolin
Created September 11, 2017 14:08
Show Gist options
  • Save thcolin/f6f7f4a5b4b66a8bc8e54adcd64fa775 to your computer and use it in GitHub Desktop.
Save thcolin/f6f7f4a5b4b66a8bc8e54adcd64fa775 to your computer and use it in GitHub Desktop.
Recursive Promise delayed
var promise = Promise.resolve()
var values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
values.forEach(function(id) {
promise = promise.then(function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(id)
}, 2000)
})
}).then(function(id) {
console.log('VALUE', id)
})
})
promise.then(function() {
console.log('FINISH')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment