如何保证异步调用是从猫鼬函数返回前执行? [英] How to ensure an asynchronous call is executed before returning from a function in Mongoose?

查看:230
本文介绍了如何保证异步调用是从猫鼬函数返回前执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的猫鼬,为了地狱prevent异步I碰到所谓的异步。我已经使用了 async.series 异步 NPM包,我的code看起来在下面

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 given format given below

var userId=1234
function findUser(callback){
  user.findOne({userId:userId}).exec(function(err,userData){
         //Some Logic
   var schoolId=123;
   var schoolName=findSchool(123);     
   findStudents(schoolName);

   var schoolId=1234;
   var schoolName=findSchool(123);     
   findStudents(schoolName);

   callback(null);
  });
}

function findSchool(schoolId){
 school.findOne({schoolId:schoolId}).exec(function(err),schoolDetails){
    //Some Logic  
  });
    var schoolName="XYZ High School"
    return(schoolName);
}

 function findStudents(schoolName){
  student.findOne({schoolName:schoolName}).exec(function(err),studentDetails{
    //Some Logic  
  });
 }

 async.series([findUser],function(err){
     // for Error Handling
    }

在这里,我使用的是异步系列函数prevent地狱异步。我打电话异步系列电话是找到我的用户用户详细信息 findUser 函数模型使用用户id 然后我打电话了 findSchool 函数的 的内部 findUser 功能,发现学校的详细信息对于给定的 SchoolId 学校模式。一旦该函数返回调用我想找到根据特定学生的 schoolName findStudents 功能学生模式。

Here I am using an async series function to prevent the ASYNC of HELL. I am calling findUser function in the async series call which is find the user details from my User model using the userId and then I am calling the findSchool function inside the findUser function which finds school details for the given SchoolId in School model. Once the function returns the call I am trying to find the students based on a particular schoolName using the findStudents function on the Student model.

此过程重复进行不同的 schoolId 。我的问题是当我叫 findSchool 功能。由于它的模型异步调用( findOne ),我能够返回的值 schoolName 甚至在 findOne 为模型。

The process is repeated for a different schoolId. My Issue comes when I call the findSchool function. Since it has a asynchronous call (findOne) on the school model, I am able to return the value of the schoolName even before the findOne is completed for the model.

有没有一种方法,以确保给定函数 findSchool 不返回的值 schoolName 直到在 school.findOne()已成功完成?我想补充更清晰我的不要的我return语句的 的对应的<$ C的 findOne $内C> findSchool 功能。

Is there a way to ensure that the given function findSchool doesn't return the value of the schoolName until the school.findOne() has completed successfully? Just to add more clarity I don't want to my return statement inside the findOne of the findSchool function.

推荐答案

您应该仍然可以使用异步,但你需要 async.waterfall 为。下面是你需要考虑的内容:

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 something
  if('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 something
  if('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 something
  if('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
  }
}

那你应该打电话给你的函数main方法:

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!');
     }
  );
}

就是这样,你必须应对方,没有回调地狱后,需要执行的一个3个功能。

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

这篇关于如何保证异步调用是从猫鼬函数返回前执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆