-
Notifications
You must be signed in to change notification settings - Fork 56
Promises API
[Back to Additional Features] (Additional-Features)
If you aren’t familiar with Promises, perhaps the Wikipedia definition will help:
In computer science, future, promise, and delay refer to constructs used for synchronizing in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially unknown, usually because the computation of its value is yet incomplete.
Put another way, a Promise represents a future value that will eventually be returned asynchronously. In the JavaScript world, the folks at CommonJS have proposed a specification called Promises/A, which is what the DeftJS Promise API is modeled upon.
In it's most basic and common form, a method will create and return a Promise like this:
// A method which uses a Store and returns a Promise
loadCompanies: function() {
var deferred = Ext.create('Deft.Deferred');
this.companyStore.load({
callback: function(records, operation, success) {
if (success) {
return deferred.resolve(records);
} else {
return deferred.reject("Error loading Companies.");
}
}
});
return deferred.promise;
}
The method which calls the above code and works with the returned Promise might look like:
// Using a Promise returned by another object.
loadCompanies: function() {
this.companyService.loadCompanies().then({
success: function(records) {
// Do something with result.
},
failure: function(error) {
// Do something on failure.
}
}).always(function() {
// Do something whether call succeeded or failed
});
}