Node.js 3函数同步,然后等待所有函数执行和结果

时间:2014-09-28 20:03:38

标签: node.js asynchronous

我需要调用3个函数,它们必须同步工作。当所有函数都完成执行时,我需要执行console.log例如。

示意图:

function one() {
    console.log('one');
}

function two() {
    console.log('two');
}

function three() {
    console.log('three');
}

one();
two();
three();

if ( ... function one, two and three were completed ... ) {
    console.log('Success!');
}

主要条件是功能同步执行。也许可以使用async库?

1 个答案:

答案 0 :(得分:2)

是的,parallel

async.parallel([one,two,three], function (err, results) {
  console.log('Success!');
});

请注意,onetwothree函数都必须接受一个回调参数,完成后它们将向其发送结果或失败。