Saturday 15 April 2017

How do I simultaneously call async functions and wait for all callbacks?

In nodeJS, how do I simultaneously call async functions and wait for all callbacks before proceed?

In the example bellow I want main to return all_results only when f1, f2 and f3 are done with the callback()

function main(){
  var all_results = [];
  f1(function(results){
    all_results.push(result)
  });

  f2(function(results){
    all_results.push(result)
  });

  f3(function(results){
    all_results.push(result)
  });

  // when all 3 calls are complete:
  return(all_results)
}


function f1(callback){
  ...
  callback(results);
}

function f2(callback){
  ...
  callback(results);
}

function f3(callback){
  ...
  callback(results);
}

Not using promises.



via Azevedo

No comments:

Post a Comment