Created
April 29, 2016 22:33
-
-
Save tablekat/fad9728843ef5b54eb0455f43c546e43 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
// node --harmony_destructuring awaito.js | |
var process = require('process'); | |
var fs = require('fs'); | |
var http = require('http'); | |
function run(generator){ | |
function resume(){ | |
var args = arguments; | |
process.nextTick(function(){ | |
itr.next.apply(itr, [args]); | |
}); | |
} | |
var itr = generator(resume); | |
itr.next(); | |
} | |
function* main(resume){ | |
console.log("Starting main"); | |
var [res1] = yield add(1, resume); | |
console.log("res1 =", res1); | |
var [err, selfSource] = yield fs.readFile('awaito.js', resume); | |
console.log("Got error:", err); | |
console.log("Myself: {", selfSource.toString().substring(0, 80).replace(/\r|\n/g,""), "}"); | |
var req = http.get("http://www.google.com/", resume); | |
var [res] = yield req; | |
var body = ""; | |
res.on('data', (chunk) => body += chunk); | |
var [nothing] = yield res.on('end', resume); | |
console.log("It's google!:", body.toString().substring(0, 80)); | |
} | |
function add(num, next){ | |
next(num + 1); | |
} | |
run(main); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by this http://stackoverflow.com/questions/32909789/why-does-generators-next-need-a-settimeout
feels monadic