JSFiddle - React, Tailwind, and code Playground
by doug65536
JavaScript
function getCustomerById(customerId, callback) {
$.post('', { action: 'getCustomerById', id: customerId }, null, 'json')
.done(function(data) {
callback(true, data.customerRecord);
}).fail(function(xhr,status,thrown) {
callback(false);
});
}
getCustomerById(123, function(ok, record) {
if (ok) {
alert(record.firstName);
} else {
alert("Error!");
}
});
/// OR
function getCustomerById(customerId) {
return $.post('', { action: 'getCustomerById', id: customerId }, null, 'json');
}
getCustomerById(123).done(function(data) {
alert(data.customerRecord.firstName);
}).fail(function(xhr,status,thrown) {
alert("Error!");
});
// WHEN:
var customer1 = getCustomerById(1), customer2 = getCustomerById(2);
$.when(customer1, customer2).done(data1, data2) { /*...*/ });