getting started with promises in angularjs
TRANSCRIPT
Getting Started withpromises in AngularJS.
What's this?
- pending
- completed.
- rejected.
The promise is the result of an asynchronous operation and may have one of the states listed below:
PENDING
REJECTED
COMPLETED* Immutable
OR
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.
● 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.
● 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).
● 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.
● 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.
● 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.
● With $routeProvider.when method:
Only will be redirected to “/allNames” when the “allNamesFactory” service promise is resolved.
Thank you!
https://github.com/cauealveshttps://twitter.com/_cauealves