12
Getting Started with promises in AngularJS.

Getting started with promises in AngularJS

Embed Size (px)

Citation preview

Page 1: Getting started with promises in AngularJS

Getting Started withpromises in AngularJS.

Page 2: Getting started with promises in AngularJS

What's this?

Page 3: Getting started with promises in AngularJS

- pending

- completed.

- rejected.

The promise is the result of an asynchronous operation and may have one of the states listed below:

Page 4: Getting started with promises in AngularJS

PENDING

REJECTED

COMPLETED* Immutable

OR

Page 5: Getting started with promises in AngularJS

In Angular, the promises are created by the service $q.

● Based on Kris Kowal promises mechanism that is called Q: https://github.com/kriskowal/q

● Used by community.

● Is integrated with the $rootScope.

● It is a basic version of the Q library containing the features required for asynchronous communication.

Page 6: Getting started with promises in AngularJS

● The Deferred API methods (just what you need to know):

- reject(reason) - Creates a promise that is resolved as rejected by the last reason as a parameter.

- resolve(value) - Resolve the promise with the value passed as a parameter.

Page 7: Getting started with promises in AngularJS

● The Promise API methods (just what you need to know):

- then(successCallback, errorCallback, notifyCallback) - calls one of the success or error callbacks asynchronously as soon as the result is available.

- catch(errorCallback) - shorthand for promise.then(null, errorCallback).

Page 8: Getting started with promises in AngularJS

● When the promise is resolved successfully, the first function passed as parameter “then” function returns a response object.

● When an error occurs, the catch function has the result of the operation.

Page 9: Getting started with promises in AngularJS

● With $http service methods:

- success - this callback will be called asynchronously when the response is available.

- error - called asynchronously if an error occurs or server returns response with an error status.

Page 10: Getting started with promises in AngularJS

● How can i synchronize multiple asynchronous functions and avoid Javascript callback problems?Answer: Use the $ q.all function, for combines multiple promises into a single promise.

Page 11: Getting started with promises in AngularJS

● With $routeProvider.when method:

Only will be redirected to “/allNames” when the “allNamesFactory” service promise is resolved.

Page 12: Getting started with promises in AngularJS

Thank you!

https://github.com/cauealveshttps://twitter.com/_cauealves