Chaining multiple asynchronous methods in javascript
<p>I am writing a javascript library to abstract ajax requests to my HTTP API.
Each of my javascript functions is a wrapper for jquery's ajax call, which makes a callback to the user on completion.</p>

<p>Eg.</p>

<pre><code>mylib.doThing( "foo", { success:function(){alert("done");});
</code></pre>

<p>In the case where I want to execute mylib.doFoo twice in series, I have something like:</p>

<pre><code>mylib.doThing( "foo", { success:function(){ mylib.doThing( "bar", { success:function(){alert("done");}); });
</code></pre>

<p>For anything more that two steps, this gets messy very quickly.</p>

<p>Is it possible to provide a cleaner syntax, perhaps more like the following? And how would I need to implement <code>mylib.doThing()</code>?</p>

<pre><code>mylib.doThing("foo").mylib.doThing("bar").alert("done");
</code></pre>
