Skip to content

Instantly share code, notes, and snippets.

@deanohyeah
Last active August 5, 2017 22:43
Show Gist options
  • Save deanohyeah/a9b12bf0d7ccbab8011ad1cb85b54914 to your computer and use it in GitHub Desktop.
Save deanohyeah/a9b12bf0d7ccbab8011ad1cb85b54914 to your computer and use it in GitHub Desktop.
iterate through a list of promises, waiting for the item to resolve before moving to the next item
function seqPromise(){
function* generator() {
let func = () => Promise.resolve()
while(true)
func = yield func()
}
const gen = generator()
return function genPush(list, fn) {
function fnWrap(fn, item) {
return () => fn(item)
}
gen.next(fnWrap(fn, list.pop())).value.then(() => {
if (list.length) {
genPush(list, fn)
}
})
}
}
const test = seqPromise()
test([1,2,3], () => Promise.resolve())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment