Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Does node.js preserve asynchronous execution order?

Does node.js preserve asynchronous execution order?

Problem

I am wondering if node.js makes any guarantee on the order async calls start/complete.

I do not think it does, but I have read a number of code samples on the Internet that I thought would be buggy because the async calls may not complete in the order expected, but the examples are often stated in contexts of how great node is because of its single-threaded asynchronous model. However I cannot find an direct answer to this general question.

Is it a situation that different node modules make different guarantees? For example at https://stackoverflow.com/a/8018371/1072626 the answer clearly states the asynchronous calls involving Redis preserves order.

The crux of this problem can be boiled down to is the following execution (or similar) is strictly safe in node?

var fs = require("fs");
fs.unlink("/tmp/test.png");
fs.rename("/tmp/image1.png", "/tmp/test.png");

According to the author the call to unlink is needed because rename will fail on Windows if there is a pre-existing file. However, both calls are asynchronous, so my initial thoughts were that the call to rename should be in the callback of unlink to ensure the asynchronous I/O completes before the asynchronous rename operation starts otherwise rename may execute first, causing an error.

Problem courtesy of: vossad01

Solution

Async operation do not have any determined time to execute.

When you call unlink, it asks OS to remove the file, but it is not defined when OS will actually remove the file; it might be a millisecond or an year later.

The whole point of async operation is that they don't depend on each other unless explicitly stated so.

In order to rename to occur after unlink, you have to modify your code like this:

fs.unlink("/tmp/test.png", function (err) {
    if (err) {
        console.log("An error occured");
    } else {
        fs.rename("/tmp/image1.png", "/tmp/test.png", function (err) {
            if (err) {
                console.log("An error occured");
            } else {
                console.log("Done renaming");
            }
        });
    }
});

or, alternatively, to use synchronized versions of fs functions (note that these will block the executing thread):

fs.unlinkSync("/tmp/test.png");
fs.renameSync("/tmp/image1.png", "/tmp/test.png");

There also libraries such as async that make async code to look better:

async.waterfall([
    fs.unlink.bind(null, "/tmp/test.png");
    fs.rename.bind(null, "/tmp/image1.png", "/tmp/test.png");
], function (err) {
    if (err) {
        console.log("An error occured");
    } else {
        console.log("done renaming");
    }
});

Note that in all examples error handling is extremely simplified to represent the idea.

Solution courtesy of: penartur

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Does node.js preserve asynchronous execution order?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×