Last active
August 5, 2017 22:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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