1

When I make an API call, I want to update 2 separate collections with MongoDB. The official way of thing this will be using transactions but because this is a prototype for now, I will just use 2 separate function calls to update each collection.

Here is what I am doing:

 async.waterfall([
    function(callback) {
        callback(null);
    }, function(callback) {
       connectToDatabase(MONGODB_URI)
      .then(db => updateDocument1(arguments))
      .then(result => {
        callback(null);
      });
    }
    , function(callback) {
       connectToDatabase(MONGODB_URI)
      .then(db => updateDocument2(arguments))
      .then(result => {
        callback(null);
      });
    , function(err, result) {
       callback(null, null)
    }
  ])

But somehow, it does not execute updateDocument2. Also, updateDocument1 stops in the middle of the process, so none of documents are updated. Do you know why? And Does anyone know a better way?

1 Answer 1

1

async parallel : do function execution parallelly without dependent of other function

async series : do function execution in series (one by one)

async waterfall : do first function execution then pass output of first function as input to next function

now in your case : if updating two collection not depending on each other you can use async parallel

Sign up to request clarification or add additional context in comments.

2 Comments

even i do parellel, none gets executed because the function prematurely exits. I am using AWS Lambda
add console log and make sure you are getting results from database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.