sync-node : a npm module which makes node synchronous without blocking the main thread

$ npm install sync-node

Though on those rising days of JavaScript there was a debate on whether asynchronous programming is good or bad, but eventually with the massive growth of JavaScript and it’s modules and the success of its frameworks, it’s kind of an established fact that asynchronous programming method has nothing bad in it. However, in many cases we see the need of synchronous tasks in JavaScript and we try to resolve them using promises etc. Today I’m gonna show you an easy way to make things synchronous, but before that let’s discuss the exact problem first.

The problem

If task A is asynchronous and task B is synchronous (or even asynchronous), than how to start executing task B only after task A is completed.

The solutions

Promise: Using promise is a nice way to handle async tasks. But what if you have 50 tasks to run one after another? May be same kind of a task, but running it 50 times with different parameters. Eg: You are scraping a hotel website for prices of next 90 days. Firing 90 requests in parallel may help the hotel website to detect and block you. So you need one request to be fired only if the previous one is complete.

Synchronous Thread: There are plugins available to make things completely synchronous. But the problem with that is, JavaScript is single threaded. So if you block the thread to wait for some response, it hangs the entire application.


The node module sync-node provides you an easy and efficient way to make things synchronous without blocking the main thread. This plugin uses the exact same mechanism used in generator tutorial.

What is sync-node?

sync-node is an npm plugin using which you can create a special queue to run your JavaScript. You can push your functions one after another in the queue and they will be executed in the same order they were pushed. If you Push function A first and then function B, than the value returned by function A will be the first argument of function B.

To install sync-node, run the following command.

An example of using sync-node

Using the function createQueue(), we are creating an empty queue. Whatever is pushed in the queue will be executed one by one. To push a job in that queue, you should use the function pushJob(). If the function you are pushing is asynchronous, than it must return a proper Promise object, else sync-node will not be able to detect and wait till the time it gets executed.

Still having issues?

If you still have issues understanding the above article, please leave a comment below and we will try to address that. In case you need help in your projects or understanding anything related to Programming; contact me, Paul Shan for assistance. Thank you for being a reader of VoidCanvas.

About This Author

Hello! I am Paul Shan, a JavaScript Expert, Full Stack and DevOps Engineer cum Consultant based out of Bengaluru, India.