deferred.promise()
Returns: Promise
Description: Return a Deferred's Promise object.
Arguments
deferred.isResolved()
version added: 1.5
The deferred.promise() method allows an asynchronous function to prevent other code from interfering with the progress or status of its internal request. The Promise exposes only the Deferred methods needed to attach additional handlers or determine the state (then, done, fail, isResolved, and isRejected), but not ones that change the state (resolve, reject, resolveWith, and rejectWith).
If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point. Return only the Promise object via deferred.promise() so other code can register callbacks or inspect the current state.
For more information, see the documentation for Deferred object.
Example
This test creates a Deferred and sets two timer-based functions to either resolve or reject the Deferred after a random interval. Whichever one fires first "wins" and will call one of the callbacks. The second timeout has no effect since the Deferred is already complete (in a resolved or rejected state) from the first timeout action.
// Create a Deferred and return its Promise
function asyncEvent(){
var dfd = new jQuery.Deferred();
setTimeout(function(){
dfd.resolve("hurray");
}, Math.floor(Math.random()*1500));
setTimeout(function(){
dfd.reject("sorry");
}, Math.floor(Math.random()*1500));
return dfd.promise();
}
// Attach a done and fail handler for the asyncEvent
$.when( asyncEvent() ).then(
function(status){
alert( status+', things are going well' );
},
function(status){
alert( status+', you fail this time' );
}
);
Was this information helpful?

