简体   繁体   English

node.js中的异步流

[英]Async flow in node.js

In my node.js application i want to make my flow synchronous. 在我的node.js应用程序中,我想使流同步。 I faced this kind of issues previously, i solved it. 我以前曾遇到过这类问题,所以解决了。 But now i am struggling now in this situation. 但是现在我在这种情况下正在苦苦挣扎。

for(var k=nearestMatchLength;k--;) {
    async.forEach(matchedArray[i].nearestmatch, function(elem,Callback){
       if(condition){
          app.models.Schedule.findById(elem.Id, function(err, res){
             for(){
             };----> for loop 
             Callback(); 
          });
       }
        Callback();
    });
}

In the above code if(condition) satisfied then the findById(which is async) is called and the Callback(); 在上面的代码中, if(condition)满足if(condition)findById(which is async)并执行Callback(); findById(which is async) after that is called before its executes. 之后在执行之前被调用。

My flow should be if it entered into if condition the fetching should be done and then only the next loop should rotate. 我的流程应该是在进入条件的情况下进行的,然后只有下一个循环旋转。

Please share your ideas. 请分享您的想法。 thanks in advance. 提前致谢。

for(var k=nearestMatchLength;k--;) {
    async.forEach(matchedArray[i].nearestmatch, function(elem,Callback){
       if(condition){
          app.models.Schedule.findById(elem.Id, function(err, res){
             for(){
             };----> for loop 
             Callback(); 
          });
       }
       else{
        Callback();
       }
    });
}

the else is added there because your app.models.Schedule.findById has a callback function(err, res) which lets the bottom Callback() be called before even getting to the function(err,res) part. else添加到那里,因为您的app.models.Schedule.findById具有回调function(err, res) ,该function(err, res)允许在到达function(err,res)部分之前调用底部的Callback()

so here is an example 所以这是一个例子

console.log('A');
setTimeout(function(){
   console.log('B');
  },0);
console.log('C');

Whats the order of the letters printed here?? 这里印刷的字母是什么顺序?

Its A, C then B 它的A,C然后是B

The example here is of setTimeout but it can be anyother function with a callback implemented. 此处的示例是setTimeout的示例,但可以是实现了回调的任何其他函数。

There is no async.forEach , and you can get rid of for loop 没有async.forEach ,您可以摆脱for循环

//async.times - execute fn number of times
async.times(nearestMatchLength, function(i, next){
    async.each(matchedArray[i].nearestmatch, function(elem, callback){
       if(true){
          app.models.Schedule.findById(elem.Id, function(err, result){
             //do something async
             //for example it's a new for loop
             async.each([1,2,3], function(number, cb) {
                //do some work
                result.number = number;
                //call cb to reach next iteration
                cb();
             }, function() {
                //all elements from [1,2,3] complete
                //do some async again and call next to complete
                //async.times execution
                result.save(next);
             });
          });
       } else {
          next(null);
       }
    });
}, function(err, done) {
    //complete
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM