Use of process.nextick in node.js

This is a post for a quick go through of node.js process.nextTick method implementation.

As we have searched around web, we found that any code snippet within node.js process.nextTick ensures that the mentioned code snippet will execute in next event loop of node.js before doing any I/O bound work.

Also we found that process.nextTick function guarantees that the code snippet within it will execute in asynchronous manner.

We had tried to show this scenario by examples.

ProcessAsync.js

 
function insertUserAsync(name, cb) {
   var username = name;
   process.nextTick(function() 
        {
		if (username == 'admin') {
			return cb(null,'Admin user insert');
		}
		else {
			return cb(null,'General user insert');
		}
	});
}

function allwork()
{
	console.log('Async msg start');
	insertUserAsync('admin',function(err,content){
		console.log('admin user call and content -'+content);
	});
	insertUserAsync('user',function(err,content){
		console.log('general user call and content -'+content);
	});
	console.log('Async msg end');

}

allwork();

Output –
Below is the output of above code, where we can see that the msg start and msg end will execute and then the snippet within process.nextTick is executing. So this calling is truly asynchronous in nature.

 
Async msg start
Async msg end
admin user call and content -Admin user insert
general user call and content -General user insert

According to node.js documentation, it is needed to be ensured that a process is either 100% Synchronous or 100% Asynchronous. In the below code, the process may be synchronous or asynchronous. So the following code may work like synchronous process or asynchronous process. But for node.js, as the main event loop runs always on single thread, any code with synchronous behavior is not a good practice. So to warn the reader, we had created the nodejs code situation, which may not be asynchronous and thus may block the main event loop of node.js. This type of code may block the main event loop for a long running I/O operation in node.js.

ProcessMayBeSync.js

 
function insertUserSync(name, cb) {
   var username = name;
   		if (username == 'admin') {
			return cb(null,'Admin user insert');
		}
		else {
			return cb(null,'General user insert');
		}
}

function allwork()
{
	console.log('sync msg start');
	insertUserSync('admin',function(err,content){
		console.log('sync admin user call and content -'+content);
	});
	insertUserSync('user',function(err,content){
		console.log('sync general user call and content -'+content);
	});
	console.log('sync msg end');

}

allwork();

Output –

In below output, we can see, the code is executing as in synchronous manner, and so it is blocking the main event loop of node.js

 
sync msg start
sync admin user call and content -Admin user insert
sync general user call and content -General user insert
sync msg end

If you find this article helpful, you can connect us in Google+ and Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *