Skip to content

Instantly share code, notes, and snippets.

@dwhitney
Created April 11, 2014 18:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwhitney/10489306 to your computer and use it in GitHub Desktop.
Save dwhitney/10489306 to your computer and use it in GitHub Desktop.
Node.js Script from Talk
var http = require('http');
var Q = require('q');
var IntensiveFunctions = (function(){
var self = {};
self.randomWalk = function(i){
return Q.fcall(function(){
var total = 0;
for(; i > 0; i--){
if(Math.random() > 0.5) total += 1;
else total += -1;
}
return total;
});
};
return self;
})();
http.createServer(function(req, res){
var promise1 = IntensiveFunctions.randomWalk(1000000000);
var promise2 = IntensiveFunctions.randomWalk(1000000000);
Q.spread([promise1, promise2], function(num1, num2){
res.writeHead(200, {'Content-Type':'text/plain'});
res.end((num1 + num2) + "\n");
});
}).listen(1337, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment