JSFiddle - React, Tailwind, and code Playground

by doug65536

JavaScript

function multiAjax(requests, callback) {
    var items = [];
    var ajax = [];
    $.each(requests, function(k, request) {
        if (request instanceof Array) {
            items.push({
                name: k,
                count: request.length
            });
            $.each(request, function(i,r) {
                ajax.push($.ajax(r, null, null, 'json'));
            });
        } else {
            items.push({
                name: k,
                count: 1
            });
            ajax.push($.ajax(request, null, null, 'html'));
        }
    });
    
    $.when.apply(window, ajax).done(function() {
        var responses = Array.prototype.slice.call(arguments, 0);
        var result = {}, multi;
        var i = 0;
        $.each(items, function(itemindex, item) {
            if (item.count == 1) {
                result[item.name] = responses[i++][0];
            } else {
                multi = [];
                for (var n = 0; n < item.count; ++n) {
                    multi.push(responses[i++][0]);
                }
                result[item.name] = multi;
            }
        });
        callback(true, result);
    }).fail(function() {
        callback(false);
    });
}

//function joinArrays(arrays) {
//    var result = [];
//    $.each(arrays, function(i,a) {
//        Array.prototype.splice.apply(result, [result.length, a.length].concat(a));
//    });
//    return result;
//}

function joinArrays(arrays) {
    var result = [];
    $.each(arrays, function(i,a) {
        result = result.concat(a);
    });
    return result;
}

console.log(joinArrays([[1,2,3],[4,5],[], [6,7]]));

// TEST ============================

multiAjax({
    //weather: [
    //    'http://www.corsproxy.com/rss.theweathernetwork.com/weather/caon0511',
    //    'http://www.corsproxy.com/rss.theweathernetwork.com/weather/caon0696'
    //],
    wiki: 'http://www.corsproxy.com/en.wikipedia.org/wiki/Ajax_(programming)'
}, function(ok, responses) {
    debugger;
   ...