Promises - AngularJS's promise provides a built-in service $q. It provide a way to execute asynchronous call in series by registering it with a promise object.
Let us now elaborate promise and deferred.
Deferred - It represent the result/output of the asynchronous operation. It elaborate the interface which are used for signaling the state and result of the operations.
Promise - It provide an interface for interacting with its related deferred and allows for interested parties to get accessed to the state and the result of the deferred operation.
While we are creating the deferred, it changes its state to resolved or reject but still we get the associated promise immediately after creating a deferred object and even assign the interaction with the future result.
But the interaction will only be occur once the deferred reject or resolved.
Lets understand how we create
1. deferred:
var firstDef = $q.defer();
2. Resolve and reject a deferred:
async(function(value){
firstDef.resolve(value);
}, function(error){
firstDef.reject(error);
})
3. Promise:
firstDef.promise
.then(function(data){
console.log("Success");
}, function(error){
console.log("Error");
})
Hope this will help you understand about the promises in angularJS
0 Comment(s)