JSFiddle - React, Tailwind, and code Playground
JavaScript
var Queue = function() {
var q = [];
var that = this;
function moveNext() {
if (q.length > 0) {
that.runItem();
}
}
this.runItem = function() {
var item = q.shift();
$.when(item.item).then([item.options.done, function() {
moveNext();
}], [item.options.fail, function() {
if (item.options.always) {
moveNext();
}
}]);
};
this.add = function(def, options) {
if ($.isArray(def)) {
for (var d in def) {
this.add(d, options);
}
return this;
}
q.push({
item: def,
options: options
});
if (q.length === 1 && !options.delay) {
this.runItem();
}
return this;
};
};
var q = new Queue();
q.add($.when().then(function() { console.log("then 1"); }), {
delay: true,
done: function done1() {
console.log("done 1");
}
});
q.add($.when().then(function() { console.log("then 2"); }), {
delay: true,
done: function done2() {
console.log("done 2");
}
});
q.runItem();