Pass ajax function to deferred

by zdam

HTML

<a href="#">Click me!</a>
<div></div>

JavaScript

$(function() {
    $("a").click(function() {
        var deferreds = GetSomeDeferredStuff();

        $.when.apply(null, deferreds).done(function() {
            $("div").append("<p>All done!</p>");
        });
    });
});

function GetSomeDeferredStuff() {
    var deferreds = [];

    var i = 1;
    for (i = 1; i <= 10; i++) {
        deferreds.push(theAjaxCall(i));        
    }    
    return deferreds;
}

function theAjaxCall(count){
    return $.post('/echo/html/', {
            html: "<p>Task #" + count + " complete.",
            delay: count
        }).success(function(data) {
            $("div").append(data);
        })            
}