Skip to content

Instantly share code, notes, and snippets.

@victorb
Created October 10, 2014 13:28
Show Gist options
  • Select an option

  • Save victorb/64f13af95dfeac86d1c2 to your computer and use it in GitHub Desktop.

Select an option

Save victorb/64f13af95dfeac86d1c2 to your computer and use it in GitHub Desktop.
var http = require('http');
var async = require('async');
function* simpleGenerator(){
for (var i = 0; i < 3; i++) {
yield i.toString();
}
}
var g = simpleGenerator();
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var interval = setInterval(function() {
nextValue = g.next();
console.log('iteration');
console.log(nextValue);
if(nextValue.done) {
res.end('\nHello World\n');
clearInterval(interval);
return
}
console.log('Writing: ' + nextValue.value);
res.write(nextValue.value);
}, 2000);
}).listen(1337, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment