Last active
March 8, 2018 17:23
-
-
Save adambene/cb3bf96a39b9f17eb602cd440a034db5 to your computer and use it in GitHub Desktop.
Coroutine runner in JavaScript
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
const coroutine = nextValue => iterator => { | |
const { done, value } = iterator.next(nextValue); | |
if (done) { | |
return; | |
} | |
if (value.constructor === Promise) { | |
value.then(promiseValue => { | |
coroutine(promiseValue)(iterator); | |
}); | |
} else { | |
coroutine(value)(iterator); | |
} | |
}; | |
// to see a working example: https://gist.github.com/adambene/b3de67803e634be8f7d6baa273b5f447 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment