How do I perform chaining of asynchronous functions in Javascript with jQuery
<p>I'm fiddling with some Javascript using jQuery. </p>

<p>I have a call to a data service which loads up a collection of 'notes'. For each 'note' I need to make a further call to get 'tags' for each of these notes. The code looks like this:</p>

<pre><code>                $.when(datacontext.notes.getData(dataOptions(force)))
                    .done(
                    function () {
                        //Once the notes are loaded you need to go through each of the notes, and load the tags.
                        _.each(notes, function (note) {
                            var noteEntitySet = datacontext.noteTagsFactory(note.id());
                            noteEntitySet.getData(note.tags)
                        });
                        }
                    )
                    .always(//Do some cleanup);
</code></pre>

<p>Now clearly the 'always' chain gets called only once the 'tag' getting calls have been initiated. But I want to callback to some function when all of the calls have returned.</p>

<p>I'm struggling to work out how I can create this asynchronous chain in Javascript. Can anyone help?</p>
