JSFiddle - React, Tailwind, and code Playground
by alnitak
JavaScript
function functionOne() {
// initiate ten requests, storing the promises in an array
var def = [];
for (var i = 0; i < 10; ++i) {
def[i] = AjaxFunction(i);
}
// wait for all the promises to complete, with the
// eventual result also being returned as a promise
return $.when.apply($, def).then(function() {
// take a copy of the arguments
var args = [].slice.call(arguments, 0);
// concatenate them all together
return args.reduce(function(p, n) {
return p + n[0];
}, "");
});
}
function AjaxFunction(t) {
return $.post('/echo/html/', {
html: '<div>' + t + '</div>'
}, 'html');
};
// NB: functionOne() is now also async - you have to wait
// for the promise to be resolved before using the result
functionOne().then(function(result) {
$(document.body).append(result);
});