NodeJS:执行多个回调时执行回调

时间:2018-04-05 15:00:15

标签: javascript node.js

这就是我需要做的事情:

function myFunc(ID, callback){
    func1("foo", res => { console.log(res) });
    func2("bar", res => { console.log(res) });
    func3("batz", res => { console.log(res) });

    // Here I need to call callback(something); when all of
    // the above funcs terminated and called their callbacks.

    // I could increment/decrement a counter to know how many
    // callbacks executed, but then how could I execute a function
    // (e.g. a callback) as soon as this counter gets to 0 ?
}

如果可能的话,我需要一个聪明而干净的解决方案(因为我已经发现计数器技巧是一种解决办法):)

谢谢!

0 个答案:

没有答案