Wednesday, November 12, 2014

When you need to be synchronous with NodeJS

NodeJS is awesome. Its asynchronous nature is a great fit for many applications.
But sometimes you want to do something synchronously, without callbacks.

An example for such a need is a little script I was working on lately. The script is supposed to download files from some web services and save them to the disk.
This kind of task doesn't fit into the async model. Many requests were sent in a short time and I supplied a callback to wait for the responses. Each request is an open socket and my machine would throw an error that there are too many open.

There are 3 possible solutions (that I see currently):
1. Restrict the number of open requests to N. When a request is done, its callback would notify the restricting resource and a new request that is waiting would take its place.
2. Just make it synchronous. No callbacks.
3. Use promises. A very elegant solution, but requires libraries to work.

I took the 2nd option and in my case, I had to find an http library for node that allows synchronous methods. So I used urllib-sync. This went well and it solved my problems.

In a different case, I didn't have the privilege of a nice library that will support synchronous methods. I needed to use Git from Node, and the libraries I checked out only allowed async methods.

So I needed to work around this and have the callback to call the next request.

This is the basic idea:

function nextAsyncCall() {
    asyncMethod(function (err, result) {
        // Some logic maybe..


This basic skeleton is completely synchronous. The next action will be performed only after the previous one is complete.

So if you are sure you need synchronous behavior - first look for a library that can help you (if you need a sync version of the X library it's usually X-sync or something similar). If there is no such library you can see which of the two solutions above fits you (pool or simply sync).

No comments:

Post a Comment