I have a Node.js app and noticed that when I madke 2 requests to my Node Api at the same time, they appear to be queued. I.e the time for the second request is the time for the first request plus the time for the second request (if I just fired that request on its own).
So I did a simple test:
app.get('/api/testpromise', function(req, res) {
sess = req.session;
var controller = new Controller(req, res, sess);
controller.testPromise()
.then(function(){
controller.testPromise()
.then(function(){
res.status(200);
res.json({});
});
})
})
The testPromise() method just does a loop:
Controller.prototype.testPromise = function(callback){
return new Promise(function(resolve, reject) {
var count = 0;
for (i = 0; i < 500000000; i++) {
count++;
}
if(count) {
resolve(count);
} else {
reject(count);
}
});
}
I fire off 2 requests to 'testpromise' at the same time from my frontend and the first takes approximately 5 seconds to complete and the approximately 10 seconds. So the second one appears to be queued behind the first.
I thought from what I had read about Node I wouldn't see behaviour like this? Node is said to be really good at taking lots of concurrent requests and handing them off. So how does this actually work?