getting started with promises in angularjs
TRANSCRIPT
![Page 1: Getting started with promises in AngularJS](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/1.jpg)
Getting Started withpromises in AngularJS.
![Page 2: Getting started with promises in AngularJS](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/2.jpg)
What's this?
![Page 3: Getting started with promises in AngularJS](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/3.jpg)
- 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](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/4.jpg)
PENDING
REJECTED
COMPLETED* Immutable
OR
![Page 5: Getting started with promises in AngularJS](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/5.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/6.jpg)
● 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](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/7.jpg)
● 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](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/8.jpg)
● 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](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/9.jpg)
● 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](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/10.jpg)
● 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](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/11.jpg)
● 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](https://reader031.vdocuments.us/reader031/viewer/2022022123/58a4b3f51a28ab2d688b4c5f/html5/thumbnails/12.jpg)
Thank you!
https://github.com/cauealveshttps://twitter.com/_cauealves