how to run two async operation in one step in node.js as one atomic operation?

I have function which call two async functions. The main function handle requests from the users. Here is example:

mainFunction(req, res, done) {
   asyncExist(req, function(exists) {
       if (!exists) {
           asyncCreate(req, done);
       }
   })
}

The problem is when I have many requests. My main function is call async. Nodejs executes function asyncExist for many request before call function asyncCreate. In result asyncCreate function is call many times with the same parameters. Question is what to do with that fundamental problem?

You might investigate using promises (like when) if you're needing to do something after several async calls complete.

See http://blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-functional-nodes-biggest-missed-opportunity/ for more info on promises...

You need a locking mechanism. I'm not sure what datastore you're using, but you need to figure out what kind of locking or transaction primitives it provides.

A lock on the datastore allows you to allow the first request make subsequent requests wait before they begin with asyncExist until the first request has completely finished.

I've used a lock pattern for redis that looked like this:

mainFunction(req, res, done) {
  asyncGetLock(function(releaseLock) {
    asyncExist(req, function(exists) {
      if(exists) {
        releaseLock();
        return;
      }
      asyncCreate(req, done, function() {
        releaseLock();
      });
    });
  });
}

Similar concept applies if you're using other datastores. For instance, in SQL it would be called a transaction. Your first request would be begin transaction then you would do your test and your conditional update and then you would commit or rollback as the final step.