Skip to content

Instantly share code, notes, and snippets.

@m3g4p0p
Created October 13, 2016 21:30
Show Gist options
  • Save m3g4p0p/2b46aa858dedc34254ef67469d4fd72f to your computer and use it in GitHub Desktop.
Save m3g4p0p/2b46aa858dedc34254ef67469d4fd72f to your computer and use it in GitHub Desktop.
A most basic generator runner
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