Node.js的之后执行功能的“N”异步回调被执行 [英] node.js execute function after 'n' async callback are executed

查看:103
本文介绍了Node.js的之后执行功能的“N”异步回调被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能让我怎么能执行后回调'的 N 的异步函数执行,例如:

  VAR after4AsyncOperation =功能(){
    //做一点事...
}
process.nextTick(函数(){
    //做东西
})
process.nextTick(函数(){
    //做东西
})
process.nextTick(函数(){
    //做东西
})
process.nextTick(函数(){
    //做东西
})

有没有执行办法 after4AsyncOperation

  VAR after4AsyncOperation =功能(){
    //做一点事...
}
process.nextTick(函数(){
    //做东西
    process.nextTick(函数(){
        //做东西
        process.nextTick(函数(){
            //做东西
            process.nextTick(函数(){
                //做东西
                after4Asyncoperation();
            })
        })
    })
})


解决方案

我会建议使用异步库如 Caolan的异步 。然后,你可以使用 async.parallel async.series async.auto 汽车是最灵活的,但比别人慢一点;你的第一个例子是 - 平行系列将需要在你想要做什么的更多信息之间的选择并行操作,但你的第二个例子是在系列。

不过,如果你不想使用一个库,你可以写一些自定义的。我会如下做到这一点:

  VAR numFuncs = 4;
VAR回调函数=(){
  numFuncs--;
  如果(numFuncs == 0){
    after4AsyncOperation();
  }
}
VAR after4AsyncOperation =功能(){
    //做一点事...
}
process.nextTick(函数(){
    //做东西
    回电话();
})
process.nextTick(函数(){
    //做东西
    回电话();
})
process.nextTick(函数(){
    //做东西
    回电话();
})
process.nextTick(函数(){
    //做东西
    回电话();
})

这工作由我们期望回调被调用的次数减计数( numFuncs ),只有调用 after4AsyncOperation 的时候,我们一直在叫回来的时间正确的号码。

I can't get how can I execute a callback after 'n' async functions are executed, example:

var after4AsyncOperation = function(){
    //do something...
}
process.nextTick(function(){
    //do stuff
})
process.nextTick(function(){
    //do stuff
})
process.nextTick(function(){
    //do stuff
})
process.nextTick(function(){
    //do stuff
})

Is there a way to execute after4AsyncOperation after the 4 async functions without have to write the function one inside the other, ex:

    var after4AsyncOperation = function(){
    //do something...
}
process.nextTick(function(){
    //do stuff
    process.nextTick(function(){
        //do stuff
        process.nextTick(function(){
            //do stuff
            process.nextTick(function(){
                //do stuff
                after4Asyncoperation();
            })
        })
    })
})

解决方案

I would suggest using an async library such as Caolan's Async. Then, you can use async.parallel, async.series, or async.auto. auto is the most flexible, but a bit slower than the others; choosing between parallel and series would require more information on what you're trying to do - your first example would be a parallel operation, but your second example is in series.

However, if you don't want to use a library, you can write something custom. I would do it as follows:

var numFuncs = 4;
var callback = function(){
  numFuncs--;
  if(numFuncs == 0) {
    after4AsyncOperation();
  }
}
var after4AsyncOperation = function(){
    //do something...
}
process.nextTick(function(){
    //do stuff
    callback();
})
process.nextTick(function(){
    //do stuff
    callback();
})
process.nextTick(function(){
    //do stuff
    callback();
})
process.nextTick(function(){
    //do stuff
    callback();
})

This works by counting down from the number of times we expect the callback to be called (numFuncs) and only calling after4AsyncOperation when we've been called back the right number of times.

这篇关于Node.js的之后执行功能的“N”异步回调被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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