Skip to content

Instantly share code, notes, and snippets.

@danschumann
Last active August 29, 2015 13:57
Show Gist options
  • Save danschumann/9880857 to your computer and use it in GitHub Desktop.
Save danschumann/9880857 to your computer and use it in GitHub Desktop.
yea node is blocking, but in weird cases like this, you can defer
// from this
app.get('/', function(req, res){
for(var i = 0; i<100000; i++) {
console.log(i); // your code here
}
res.send(true);
});
//to this
app.get('/', function(req, res){
var done = function () {
res.send(true);
}
var i=0;
var iterate = function () {
console.log(i); // Your code here
if (i++ < 100000)
// keep going
setTimeout(iterate, 0);
else
done();
}
// start it
iterate();
});
@danschumann
Copy link
Author

instead of setTimeout(iterate,0), process.nextTick(iterate) is probably faster and better

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