JSFiddle - React, Tailwind, and code Playground
HTML
<h1>Test1</h1>
<p id="test1"></p>
<h1>Test2</h1>
<p id="test2"></p>
JavaScript
function a(counter, $target) {
var $deferred = $.Deferred();
setTimeout(function () {
$target.text($target.text() + ' ' + counter);
$deferred.resolve();
}, 1000);
return $deferred;
}
function Test1() {
return $.when($.map([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function (val, i) {
return a(i, $('#test1'));
}));
}
function promiseLoop(arr, callback) {
return arr.reduce(function (previousPromise, val, i) {
return previousPromise.then(function (resultArray) {
return $.when(callback(val, i)).then(function (res) {
return resultArray.concat([res]);
});
});
}, $.when([]));
}
function Test2() {
return promiseLoop([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function (val, i) {
return a(i, $('#test2'));
});
}
// Run
Test1().done(function () {
$('#test1').css('font-weight', 'bold');
}).fail(function () {
alert('fail');
});
Test2().done(function () {
$('#test2').css('font-weight', 'bold');
}).fail(function () {
alert('fail');
});