http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/
function getCustomerSSNById(customerId){
var deferred = $.Deferred();
setTimeout(function() {deferred.resolve({"ssn" : "111 44 9999"});}, 300);
return deferred.pipe(function(p){
return p.ssn;
});
}
function getPersonAddressBySSN(ssn){
var deferred = $.Deferred();
console.log("ssn is " + ssn);
setTimeout(function() {deferred.resolve({"address" : "123 blah st"});},1000);
return deferred.pipe(function(p){
return p.address;
});
}
function getPersonAddressById(id){
return getCustomerSSNById(id).pipe(getPersonAddressBySSN);
}
getPersonAddressById(123).done(function(a){
alert("The address is " + a);
});
And using when
$.when(getCustomerSSNById(123), getPersonAddressBySSN("123 45 6789"))
.done(function(person, address){
alert("The name is " + person.ssn + " and the address is " + address);
});
No comments:
Post a Comment