JSFiddle - React, Tailwind, and code Playground
HTML
<a href="#" class="ajax">ajax</a>
JavaScript
/*Deferred Object
As of jQuery 1.5, the Deferred object provides a way to register multiple callbacks into self-managed callback queues, invoke callback queues as appropriate, and relay the success or failure state of any synchronous or asynchronous function.*/
function _ajax(id){
return $.ajax({
url: '/echo/json/',
type: 'GET',
dataType: 'json',
data: {param1: id},
complete: function(xhr, textStatus) {
//called when complete
},
success: function(data, textStatus, xhr) {
//console.log(data)
//called when successful
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
});
}
// merge response data from both ajax calls when they are done
$.when( _ajax(1), _ajax(2) ).done(function(a1, a2){
console.log(a1, a2);
});