title: Are callbacks always asynchronous?
body: <p>This example is JavaScript, since that's where I'm using callbacks mostly.  I want to understand how they work at a low level.</p>
  
  <p>In the example below, I'd expect everything to happen in order and "calling back" to occur after "step 3" and before "step 4."  This makes sense to me, as everything is done in order on a single thread of execution.  There's no trickery.  The only thing that is sort of special is that you've passed a function to another function.</p>
  
  <pre><code>function main() {
    console.log("step 1");
    console.log("step 2");
    doSomething(myCallBack);
    console.log("step 4");
  }
  
  function doSomething(f) {
    accessTheDatabase(); // this could take a while
    console.log("step 3");
    f(); // done - now call back
  }
  
  function myCallBack() {
    console.log("calling back!");
  }
  </code></pre>
  
  <p>How would you make <code>doSomething</code> asynchronous so that "step 4" can be executed before, or in parallel with, "step 3"?  </p>
  
  <p>I assume that if <code>doSomething</code> were somehow called asynchronously, it would have to be on a diffferent thread, no?  And if so, when it finishes and then calls <code>myCallBack</code>, <strong>does the callback happen on the second thread or on the main thread?</strong>  If it happens on the main thread, why does the second thread even need a pointer to the callback function?  How does the inter-thread call work?</p>