My understanding of node.js is that the process exits when callback queue is empty, meaning all the callbacks are executed. But, I noticed some node APIs callback will never get called in some scenarios. How would node know when to exit the process? Would none of my callbacks never get called in some scenario?
var net = require('net');
var client = net.connect({port: 8080}, function () {// first callback
console.log('connected');
});
client.on('error', function () {// second callback
console.log('error');
});
// end of script
I tried the example code above, the process would still exit, even it cannot make connect. But, the first callback never gets called. In this case, how would node know that the first callback may not get called, so the process would still exit instead of just hanging there?
My educated guess is that, those callbacks are event callbacks, so they are not considered normal callbacks. If node know that these callbacks may not get called, would it happened that node exit the process before neither of my callback gets called in some scenarios? (neither 'connected', nor 'error' are printed)