title: Getting synchronous behavior in javascript?
body: <p>While developing a mobile PhoneGap app I had an interesting problem. I needed to query about 10 items of data from the database (through PhoneGaps SQLite API)... Like many javascript API's, this one was asynchronous. When you made your query, you would pass in a "success" handler.</p>
  
  <p>Now, my preference in this case would have been a synchronous query method that returned only when complete. Then I could have written straight line code that queried each of the 10 items 1 after another.</p>
  
  <p>Because of the asynchronous nature of PhoneGap (really, I see this all over JS however) I was forced to write a beast that looked like this:</p>
  
  <pre><code>db.query( "SELECT...", success() {
      db.query( "SELECT...", success() {
          db.query( "SELECT...", success() {
              db.query( "SELECT...", success() {
                  db.query( "SELECT...", success() {
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>And this is only half as deep as I had to go (and greatly simplified...)... When, had I been using SQLite in C, I could have simply done something like:</p>
  
  <pre><code>db.query( "SELECT...", resultA );
  db.query( "SELECT...", resultB );
  db.query( "SELECT...", resultC );
  db.query( "SELECT...", resultD );
  db.query( "SELECT...", resultE );
  </code></pre>
  
  <p>It seems to me that the success handler approach is great when you only have to go 1 or 2 levels deep... But, completely falls apart when you need more than that...</p>
  
  <p>Is their a library, or feature of a library somewhere that makes this easier? </p>