Skip to content Skip to sidebar Skip to footer

How To Ensure An Asynchronous Call Is Executed Before Returning From A Function In Mongoose?

I am new to mongoose and in order to prevent ASYNC OF HELL I came Across an npm package called async. I have used the async.series of the async npm package and my code looks in the

Solution 1:

You should still use async but you need async.waterfall for that. Here is what you need to consider:

A main method to call your async function:

var getInformation = function(){
  async.waterfall([
    //Array of your functions in order, we will be back here later
  ], function (err) {
       if(err){
         console.log(err);
       }else{
         console.log('Everything OK!');
     }
  );
}

Then you need your functions to be async friendly, that means you should use callbacks and give your data from one function to another. Something like this:

function findUser(callback){
  //Do somethingif('Everything OK'){
    callback(err, yourData); //err should be null if everything is OK and yourData should be the data that you wanna use in your next function. e.g. schoolId 
  }else{
    callback(err); //Something was wrong, "err" should have something different to null
  }
}

function findSchool(callback, schoolId){ //Note that we receive the parameter schoolId here but not in the first function//Do somethingif('Everything OK'){
    callback(err, yourData); //err should be null if everything is OK and yourData should be the data that you wanna use in your next function. e.g. schoolName 
  }else{
    callback(err); //Something was wrong, "err" should have something different to null
  }
}

function findStudents(callback, schoolName){
  //Do somethingif('Everything OK'){
    callback(err); //err should be null if everything is OK if this is the last function maybe we don't need to send back more data from here 
  }else{
    callback(err); //Something was wrong, "err" should have something different to null
  }
}

Then you should call your functions in your main method:

var getInformation = function(){
  async.waterfall([
    findUser,
    findSchool,
    findStudents
    //Note that there is no need to tell the functions the parameters they are sending or receiving here
  ], function (err) {
       if(err){
         console.log(err);
       }else{
         console.log('Everything OK!');
     }
  );
}

And that's it, you have 3 functions that should be executed one after the other and no callback hell is needed.

Solution 2:

Async.each or async.waterfall should do the trick, I think.

The main thing here is that you're using return statements in your async functions, which will return before the async function has actually completed, and thus they won't pass along what you need.

You have to use async functions and callbacks for this. You can still have your functions separated like you want in your code, but you'll have to add callback functions instead of returns.

Post a Comment for "How To Ensure An Asynchronous Call Is Executed Before Returning From A Function In Mongoose?"