Skip to content

Instantly share code, notes, and snippets.

@adambene
Last active March 8, 2018 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adambene/cb3bf96a39b9f17eb602cd440a034db5 to your computer and use it in GitHub Desktop.
Save adambene/cb3bf96a39b9f17eb602cd440a034db5 to your computer and use it in GitHub Desktop.
Coroutine runner in JavaScript
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