Created
October 13, 2016 21:30
-
-
Save m3g4p0p/2b46aa858dedc34254ef67469d4fd72f to your computer and use it in GitHub Desktop.
A most basic generator runner
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 run (iterator) { | |
// Kick off the promise chain | |
Promise.resolve().then( | |
function resolved (value) { | |
// Get the next yielded iterator step, | |
// passing in the value from the last | |
// fulfillment | |
const next = iterator.next(value) | |
// If done, return | |
if (next.done) return | |
// Cast the next value to a promise | |
Promise.resolve(next.value).then( | |
// If resolved, recursively go to | |
// the next step | |
resolved, | |
// If rejected, throw the reason back | |
// to the iterator so that it can get | |
// catched there | |
reason => { | |
iterator.throw(reason) | |
} | |
) | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment