user2058653
user2058653

Reputation: 733

Node.js 3 functions synchronous, then wait for all functions execution and result

I need to call 3 functions, which must work synchronously. When all functions will finish execution, then I need to execute console.log for example.

Schematic example:

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

The main condition is that the functions performed synchronously. Maybe it is possible to do with async library?

Upvotes: 0

Views: 66

Answers (1)

Valentin Waeselynck
Valentin Waeselynck

Reputation: 6051

Yes, with parallel :

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

Note that the one,two, three functions must all accept a callback argument, into which they will send their result or failure when completed.

Upvotes: 2

Related Questions