title: Sequentially execute the functions? Not sure how to precisely describe the question
body: <p>I run into a problem trying to solve it; but somehow stuck in the middle. The problem is to implement a function $do() which could take any number of functions and execute them in the order they were called.
  In the specific example, print a in 700 ms and b in 200, then print c in 100ms. The challenge part is although c should be print first, but it is called after in the $do() after a and b get called. 
  My implementation as below, almost working, but cannot print c for some reason. Help needed. Thanks a lot.</p>
  
  <pre><code>const a = (cb) =&gt; {
      setTimeout(() =&gt; {
          cb();
          console.log('a', 700);
      }, 700);
  };
  
  const b = (cb) =&gt; {
      setTimeout(() =&gt; {
          cb();
          console.log('b', 200);
      }, 200);
  };
  
  
  const c = (cb) =&gt; {
      setTimeout(() =&gt; {
          cb();
          console.log('c', 100);
      }, 100);
  };
  
  const stores = [];
  let running = false;
  
  function $do() {
      const cbs = Array.prototype.slice.call(arguments);
      stores.push(cbs);
      let i = 0;
  
      while (stores.length &gt; 0 &amp;&amp; !running) {
          let head = stores.shift();
          running = i &lt; head.length;
  
          head.forEach((cb) =&gt; {
              cb(() =&gt; {
                  i++;
                  running = i &lt; head.length;
                  console.log(running);
              });
          });
      }
  }
  
  $do(a, b);
  $do(c);
  </code></pre>