Skip to content

Instantly share code, notes, and snippets.

@kypflug
Created September 25, 2015 23:28
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 kypflug/7556530ff3b5b40c3753 to your computer and use it in GitHub Desktop.
Save kypflug/7556530ff3b5b40c3753 to your computer and use it in GitHub Desktop.
function spawn(genF, self) {
return new Promise(function (resolve, reject) {
var gen = genF.call(self);
function step(nextF) {
var next;
try {
next = nextF();
} catch (e) {
// finished with failure, reject the promise
reject(e);
return;
}
if (next.done) {
// finished with success, resolve the promise
resolve(next.value);
return;
}
// not finished, chain off the yielded promise and `step` again
Promise.resolve(next.value).then(function (v) {
step(function () { return gen.next(v); });
}, function (e) {
step(function () { return gen.throw(e); });
});
}
step(function () { return gen.next(undefined); });
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment