JSFiddle - React, Tailwind, and code Playground
JavaScript
function DeferredAjax(opts) {
this.options = opts;
this.deferred = $.Deferred();
this.country = opts.country;
}
DeferredAjax.prototype.invoke = function() {
var self = this,
data = {
country: self.country
};
console.log("Making request for [" + self.country + "]");
return $.ajax({
type: "GET",
url: "/echo/json/",
data: data,
dataType: "JSON",
success: function() {
console.log("Successful request for [" + self.country + "]");
self.deferred.resolve();
}
});
};
DeferredAjax.prototype.promise = function() {
return this.deferred.promise();
};
var countries = ["US", "CA", "MX"], startingpoint = $.Deferred();
startingpoint.resolve();
$.each(countries, function(ix, country) {
var da = new DeferredAjax({
country: country
});
$.when(startingpoint ).then(function() {
da.invoke();
});
startingpoint= da;
});