title: Callback javascript function parallel
body: <p>I'm using this function to do a callback, the task is to not modify the 'f' function. The idea is to start all functions(parallel) and then end all functions and display 'Done' at the end.(runing in node).</p>
  
  <pre><code>function f(cb) {
    console.log("f's activity starts.");
    var t = Math.random() * 500; //gives a num between 0 and 1000
  
    function onActivityDone() {
      console.log("f's activity ends.");
      if (cb) cb();
    }
    setTimeout(onActivityDone, t);
  
  }
  
  function final() {
    console.log('Done');
  }
  
  function first() {
    final();
  }
  
  
  
  f()
  {     
  f()
  {
      f(final)
    };
  };
  </code></pre>
  
  <p>This is how the output is supposed to look.</p>
  
  <pre><code>f's activity starts.
  
  f's activity starts.
  
  f's activity starts.
  
  f's activity ends.
  
  f's activity ends.
  
  f's activity ends.
  
  Done.
  </code></pre>
  
  <p>Sometimes I got that output but not always, most of the time looks like this </p>
  
  <p>f's activity starts.</p>
  
  <pre><code>f's activity starts.
  
  f's activity starts.
  
  f's activity ends.
  
  f's activity ends.
  
  Done.
  
  f's activity ends.
  </code></pre>
  
  <p>and I have no idea why :/</p>
  
  <p>Any ideas why..</p>
  
  <p>Thank you !</p>