jQuery $ .each嵌套在$ .When中

I have a list of itens to pass through a function that returns a $.ajax() but the .done is getting called after the $.each is called, not after the inners ajax calls complete! What should I do?!?

Here is my code:

$.when(
    $.each(data, function (index, item) {
        GetReservation(
            item.UniqueId,
            apiRoot,
            //this is the 'success' function
            function (data2) {
                //do my stuff
            },
            //this is the 'error' function
            function (x, y, z) {
                if (x.status == 404) {
                    //OK, no reservations!
                }
            });
    })
).done(setButtons(box, c));

$.each is not returning an array of promises as your code seems to be expecting. You should build an array of promises first then call $.when with your array:

var promises = [];

$.each(data, function (index, item) {
    // add promise to array
    promises.push(GetReservation(
        item.UniqueId,
        apiRoot,
        //this is the 'success' function
        function (data2) {
            //do my stuff
        },
        //this is the 'error' function
        function (x, y, z) {
            if (x.status == 404) {
                //OK, no reservations!
            }
        }
    ));
})

$.when.apply($, promises).done(function(){
  setButtons(box, c)
});