Skip to content

Instantly share code, notes, and snippets.

@edwardgeorge
Last active October 2, 2015 21:45
Show Gist options
  • Save edwardgeorge/fd359bc411d5d5d2795e to your computer and use it in GitHub Desktop.
Save edwardgeorge/fd359bc411d5d5d2795e to your computer and use it in GitHub Desktop.
// when I have to write some node for the first time ...
Promise.fromGen = function (gen, v) {
return new Promise(function (resolve) {
var cont = function (g, v) {
var x = g.next(v);
if (x.done) { return resolve(x.value); }
return x.value.then((v) => cont(g, v));
};
cont(gen(v));
});
}
Promise.prototype.feedGen = function (gen) {
return this.then((v) => Promise.fromGen(gen, v));
};
Promise.resolve("!").feedGen(function* (v) {
var x = yield Promise.resolve("hello");
var y = yield Promise.resolve("world");
return x + ", " + y + v;
}).then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment