Skip to content

Instantly share code, notes, and snippets.

@tablekat
Created April 29, 2016 22:33
Show Gist options
  • Save tablekat/fad9728843ef5b54eb0455f43c546e43 to your computer and use it in GitHub Desktop.
Save tablekat/fad9728843ef5b54eb0455f43c546e43 to your computer and use it in GitHub Desktop.
// 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);
@tablekat
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment