Friday 6 November 2015

Using promises and $q to handle asynchronous calls in angularjs

Hello,

Here i am introducing how to handle asynchronous calls in angularjs , take a look here-

$q-




  1. - service in module ng

A service that helps you run functions asynchronously, and use their return values (or exceptions) when they are done processing.

This is an implementation of promises/deferred objects inspired by Kris Kowal's Q.

The CommonJS Promise proposal describes a promise as an interface for interacting with an object that represents the result of an action that is performed asynchronously, and may or may not be finished at any given point in time.
From the perspective of dealing with error handling, deferred and promise APIs are to asynchronous programming what try, catch and throw keywords are to synchronous programming.


Example-
For the purpose of this example let's assume that variables `$q` and `GetValue`
are available in the current lexical scope (they could have been injected or passed in).

function GetName(name)
var deferred = $q.defer(); 
setTimeout(function() { 
deferred.notify('About to greet ' + name + '.'); 
if (GetValue(name)) { 
 deferred.resolve('Hello, ' + name + '!'); } 

else { 

deferred.reject('Greeting ' + name + ' is not allowed.');  } 

}, 
1000):

return deferred.promise;}var promise = GetName('Robin Hood');

promise.then(function(greeting) {

  alert('Success: ' + greeting);},

function(reason) { 

alert('Failed: ' + reason);},

function(update) { 

 alert('Got notification: ' + update);});





At first it might not be obvious why this extra complexity is worth the trouble. The payoff comes in the way of guarantees that promise and deferred APIs make.

Additionally the promise api allows for composition that is very hard to do with the traditional callback (CPS) approach.

The Deferred API-

A new instance of deferred is constructed by calling $q.defer(). The purpose of the deferred object is to expose the associated Promise instance as well as APIs that can be used for signaling the successful or unsuccessful completion, as well as the status of the task. Methods-
  • resolve(value) – resolves the derived promise with the value. If the value is a rejection constructed via $q.reject, the promise will be rejected instead.
  • reject(reason) – rejects the derived promise with the reason. This is equivalent to resolving it with a rejection constructed via $q.reject.
  • notify(value) - provides updates on the status of the promise's execution. This may be called multiple times before the promise is either resolved or rejected.
Properties-
  • promise – {Promise} – promise object associated with this deferred.

The Promise API-

A new promise instance is created when a deferred instance is created and can be retrieved by calling deferred.promise.
The purpose of the promise object is to allow for interested parties to get access to the result of the deferred task when it completes.
Methods-
  • then(successCallback, errorCallback, notifyCallback) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected. This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback (unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining). It also notifies via the return value of the notifyCallback method. The promise cannot be resolved or rejected from the notifyCallback method.
  • catch(errorCallback) – shorthand for promise.then(null, errorCallback)
  • finally(callback, notifyCallback) – allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved.
Thanks.........

No comments:

Post a Comment