Created
April 11, 2014 18:15
-
-
Save dwhitney/10489306 to your computer and use it in GitHub Desktop.
Node.js Script from Talk
This file contains 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
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