Script for dynamically-loading data onto a restaurant menu for printing
Piece iterates over list, calling same $.ajax(), err or, other functions, etc., for each item within list. Save list items to _list , poll until _list.length === list.length - all list items processed - display "menu ready" to viewer.

JSFiddle

(function menus() {

var list = ["prestarters"
           , "starters"
           , "mains"
           , "sides"
           , "dessert"
           , "dessertwines"
           , "afterdinnerdrinks"
           , "whiskeys"
           , "hotdrinks"
           , "ports"
           , "cognacs"
           , "liqueurs"];

var err = function (jqxhr, textStatus, errorThrown) {
    if (textStatus == 'error') {
        var msg = "Sorry but there was an error: ";
        $(".s")
            .html(msg + textStatus + " " + errorThrown);
    };
};

var _list = [];

$.each(list, function (index, value) {
    $.when(value)
        .then(function (data) {
        // var _ids = "#" + data;
        // var _urls = data + ".php";
        // console.log(_ids, _urls);
        // i.e.g.,
        // `$.get(data + ".php")`
        $.post("/echo/json/", {
            json: JSON.stringify([value])
        }, "json")
            .then(function (response, textStatus, jqxhr) {
            _list.push(response);
            // `html`
            $("&lt;section&gt;", {
                    "id": response[0],
                    "html": response[0] + "&lt;br /&gt;"
            }).appendTo("#menu");
        }, err)
            .always(function (response, textStatus, jsxhr) {
                // `always` complete callback,
                // when `list` complete
            if (_list.length === list.length) {
                $("&lt;data&gt;" + _list.length + " menus ready&lt;/data&gt;")
                    .css({
                    "position": "absolute",
                    "left": "200px",
                    "top": "32px",
                    "fontSize": "28px",
                    "color": "blue"
                })
                .prependTo("#menu") &amp;&amp; $("data").fadeOut(7500, function () {
                    $("#menu data").remove()
                });
                // alert(_list.length + " menus ready")
            };
        })
    })
});
}());