I want to test the asynchronous performance of sails.js, so I wrote the following code to wait for 10 seconds (I think the code should be executed asynchronously due to the callback function). When I visited the url which executes the code, it waited 10s, it is fine.
But meanwhile, I visited a different url which only executes res.view(anohterView), it still waited for 10s. Does that mean sails.js execute synchronously, and the second url visiting is blocked by the first one? Thanks!
someModel.findOne(modelId, function foundModel (err, model) {
if (err) return next(err);
if (!model) return next('Model doesn\'t exist.');
function sleep(milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
}
sleep(10000);
res.view(Someview);
});
Your sleep function is blocking. Sails.js/nodejs is single-threaded so when your callback gets called, the thread is going to be blocked until it's finished.
If you want to 'sleep', you should be using setTimeout:
someModel.findOne(modelId, function foundModel (err, model) {
if (err) return next(err);
if (!model) return next('Model doesn\'t exist.');
setTimeout(
function() {
res.view(Someview);
}
10000
);
});