What is asynchronous process in nodejs?. Take a look at my testing.
+ Person.find(): query on database, it will take time
+ while(): delay for 5s
And the result in console like this serial:
the first: Listening on port 3000
the second: Wait
----third: 0
---------: 4
---------: 2
---------: 2
---------: 3
---------: 1
---------: 1
If we talk this is asynchronous process, why the program stop at while() function in 5s before run console.log('0') and console.log ('4')?
var Product = mongoose.model('Product', ProductSchema);
var myObject = new Object();
Person.find().exec(function (err, docs) {
for (var i=0;i<docs.length;i++)
{
Product.find({ user: docs[i]._id},function (err, pers) {
myObject[i] = pers;
console.log('1');
});
console.log('2');
}
console.log('3');
});
console.log('Listening on port 3000');
var startTime = new Date().getTime();
console.log('wait');
while (new Date().getTime() < startTime + 5000); //the program stop here for 5s before priting
console.log('0'); //console.log('0') and console.log('4');
app.listen(3000);
console.log('4');
The reason you're getting this order of execution is because the only asynchronous functions you're showing are Person.find().exec()
and Product.find()
.
The order you're seeing is this:
Person.find().exec()
is asynchronous and doesn't block the main thread.console.log('Listening on port 3000')
runs.console.log()
is synchronous, so var startTime
is set.console.log('wait');
is synchronous and continues after execution.while()
loop runs. It blocks the main thread.console.log('0');
.listen()
and console.log('4')
functions are both synchronously run.Person.find().exec()
finally runs and starts the for
loop.for
loop is also blocks. All iterations complete before continuing.console.log('3')
is run.As a summary, your program stops at the while()
loop because the loop is blocking. If you want to delay execution of code, do so without blocking the main thread by using one of the global timer functions:
setTimeout(function() {
console.log('0');
app.listen(3000);
console.log('4');
}, 5000);