JSFiddle - React, Tailwind, and code Playground
JavaScript
// Just a counter for our errors that'll stand-in for a 401
// for a couple of times
var errors = 2;
function doSomething() {
var dfr = $.Deferred(),
errorCount = 0;
(function makeRequest() {
$.ajax({
// Force an error a couple of times, then point
// to a URL that's going to succeed for testing
url: errors-- === 0 ? "/echo/json/" : "foo.bar",
dataType: "json",
success: dfr.resolve,
error: function( jqXHR ) {
if ( jqXHR.status === 404 ) {
console.log( "Error #:" + ( ++errorCount ));
return makeRequest( this );
}
dfr.rejectWith.apply( this, arguments );
}
});
}());
return dfr.promise();
}
doSomething().done(function() {
console.log( "Finally, a success!" );
});