Does node.js preserve asynchronous execution order?

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 http://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.

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.

If you look at the documentation of Node.js you'll find that the function fs.unlink takes a callback as an argument as:

 fs.unlink(path, [callback]);

An action that you intend to take when the current function returns should be passed to the function as the callback argument. So typically in your case the code will take the following form:

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

In the specific case of unlink and rename there are also synchronous function in Node.js and can be used as fs.unlinkSync(path) and fs.renameSync(oldPath, newPath). This will ensure that the code is run synchronously.

Moreover if you wish to use asynchronous implementation but retain better readability you could consider a library like async. It also has options for different modes of implementation like parallel, series, waterfall etc.

Hope this helps.