title: Pattern for wrapping an Asynchronous JavaScript function to make it synchronous
body: <p>I'm working with a JavaScript API where most of the functions are asynchronous.  The API is the <a href="http://webkit.org/blog/126/webkit-does-html5-client-side-database-storage/" rel="noreferrer">WebKit JavaScript Database API</a> which is a binding to a subset of functionality to manipulate SQLite3 databases.  I understand the design decision to make things async as to not block and provide a responsive user interface.  In my situation I know that my usage of the async API calls will execute fast.  Since this is the case I'd like to provide my developers a cleaner and easier to use wrapper API that forces synchronous calls.</p>
  
  <p>Here's the async call</p>
  
  <pre><code>db.executeSql(sqlStatement, function(result) {
    // do something with result
  });
  </code></pre>
  
  <p>And here's what I'd like to be able to do</p>
  
  <pre><code>var result = dbWrapper.executeSql(sqlStatement);
  // do something with result
  </code></pre>
  
  <p>Is there a design pattern/way to do this?  A written or linked to code example is preferred.  The target platform/broswer is Mobile Safari on the iPhone.</p>
  
  <p>Thank you</p>