Last active
August 29, 2015 13:57
-
-
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
This file contains hidden or 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
// 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(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
instead of
setTimeout(iterate,0)
,process.nextTick(iterate)
is probably faster and better