Q: How can I execute pieces of javascript code asynchronously and wait their results? I think in this case an example is better than 1000 words:
function fib(num) {
    if (num <= 1)
        return 1;

    return fib(num-1) + fib(num-2);
}

function asyncFib(num) {
    if (num <= 1)
        return 1;

    var a = asyncCall(fib, [num-1]);
    // don't wait for fib(n-1)
    // and perform fib(n-2) in 'parallel'
    var b = asyncCall(fib, [num-2]);

    // wait the results for fib(n-1) and fib(n-2) and use them
    return a + b;
}

The Fibonacci algorithm is just an example, I would like to learn how to add concurrency in javascript for 'divide and conquer algorithms'. I found that node.js and async package could be an answer but i didn't realize how.
Can somebody help me?
Thank you!

A: You can use PromiseJs
It a simple example :
var Promise = require('promise');

function fib(num) {
return new Promise(function(resolve,reject){
 if (num <= 1)
    resolve(1);

 resolve(fib(num-1) + fib(num-2));
});

}

function asyncFib(num) {
 if (num <= 1)
    return 1;
 var promiseAResolved = false;
 var promiseBResolved = false;

 var a = asyncCall(fib, [num-1]).then(function(result){
  promiseAResolved = true;

  if(promiseAResolved  === true && promiseBResolved  === true){
   return a + b;
  }

 });

 var b = asyncCall(fib, [num-2]).then(function(result){
  promiseBResolved = true;

  if(promiseAResolved  === true && promiseBResolved  === true){
   return a + b;
  }

 });


 return a + b;
}

More information : https://github.com/then/promise
and Q.js : https://github.com/kriskowal/q
