Deferred
by Akram kamal
JavaScript
function getCustomer(customerId){
var d = $.Deferred();
$.post(
"/echo/json/",
{json: JSON.stringify({firstName: "Jose", lastName: "Romaniello", ssn: "123456789"})}
).done(function(p){
d.resolve(p);
}).fail(d.reject);
return d.promise();
}
function getPersonAddressBySSN(ssn){
return $.post("/echo/json/", {
json: JSON.stringify({
ssn: "123456789",
address: "Siempre Viva 12345, Springfield" })
}).pipe(function(p){
return p.address;
});
}
$.when(getCustomer(123), getPersonAddressBySSN("123456789"))
.done(function(person, address){
alert("The name is " + person.firstName + " and the address is " + address);
});