Last active
October 2, 2015 21:45
-
-
Save edwardgeorge/fd359bc411d5d5d2795e to your computer and use it in GitHub Desktop.
This file contains 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
// 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