title: Understanding Javascript callbacks
body: <p>I am reading various tutorials on javascript with callbacks, but they are not clarifying a basic principle. What I understand is that when a function is called with a callback, this makes it possible to wait on execution of some code, and after that execute the callback function. So</p>
  
  <pre><code>// function definition
  do_func(int num, callback)
  {
     Console.log(num);
     ..
     ..
     callback();
  }
  
  //call the function
  do_func(123, function(){
     Console.log("Running callback");
  });
  </code></pre>
  
  <p>So during execution of do_func, all lines of code are executed, and then callback() is executed. However if our function changed as</p>
  
  <pre><code>    // function definition
      do_func(int num, callback)
      {
         read file...
         ..
         ..
         callback();
      }
  </code></pre>
  
  <p>Then the callback would be called while the file is being read. So our purpose is defeated. What is happening here?</p>
  
  <p>Thanks very much!</p>