JSFiddle - React, Tailwind, and code Playground
JavaScript
var tasks = [
function(){
var d = $.Deferred();
setTimeout(function(){
console.log(1);
d.resolve();
}, Math.floor(Math.random()*3000));
return d.promise();
},
function(){
var d = $.Deferred();
setTimeout(function(){
console.log(2);
d.resolve();
}, Math.floor(Math.random()*3000));
return d.promise();
},
function(){
var d = $.Deferred();
setTimeout(function(){
console.log(3);
d.resolve();
}, Math.floor(Math.random()*3000));
return d.promise();
}
];
function deferredSequentialDo(tasks, index){
if(typeof (index) === 'undefined'){
index = 0;
}
if(index < tasks.length){
$.when(tasks[index]()).then(function(){
deferredSequentialDo(tasks, index + 1);
});
}
}
deferredSequentialDo(tasks);