1. $q
$Q is a built-in service for angular that enables you to perform functions asynchronously and allows you to use the return value (or exception) of a function when the function is executed.
2. Defer
The literal meaning of defer is delay, $q. Defer () You can create a deferred instance (a deferred object instance).
The deferred instance is designed to expose a derived promise instance, as well as a signal API that is used as a successful or unsuccessful completion, and the status of the current task. This sounds complicated, and the relationship between $q, defer and promise is shown below.
var deferred = $q. Defer (); Registers a deferred object via the $q service deferred
var promise = deferred.promise;//A commitment deferred can be obtained by promise delay object, and promise returns the completion result of the current task
The Defer method:
- Deferred.resolve (value) successfully resolved (resolve) its derived promise. Parameter value will be used in future as Promise.then (Successcallback (value) {...}, errorcallback (reason) {...}, Notifycallback (notify) {...}) The parameters of the Successcallback function in.
- Deferred.reject (reason) did not successfully resolve its derived promise. The parameter reason is used to describe the cause of the unsuccessful failure. The Promise object of the deferred instance captures an error that the task did not execute successfully, Promise.catch (errorcallback (reason) {...}). To add, Promise.catch (errorcallback) is actually shorthand for promise.then (null, errorcallback).
- Notify (value) updates the execution status of promise (bad translation, the exact words are provides updates on the status of the promise ' s execution)
A small example of defer:
function Asyncgreet (name) {
var deferred = $q. Defer ();//Create a deferred delay object by $q.defer (), when creating a deferred instance, A derived promise object is also created, using Deferred.promise to retrieve the derived promise.
deferred.notify (' About to greet ' + name + '. ');//Delay object's Notify method.
if (oktogreet (name)) {
deferred.resolve (' Hello, ' + name + '! ');//task successfully executed
} else {
deferred.reject (' Greeting ' + name + ' is not allowed '); The task was not successfully performed
}
return deferred.promise//Returns deferred instance Promise object
}
function Oktogreet (name) {
Only mock data, the actual situation will be based on the relevant business implementation code
if (name = = ' Superman ') return true;
else return false;
var promise = Asyncgreet (' Superman ');
the then function that obtains the Promise object//promise object obtains the current task, which is the execution state of the current deferred deferred instance. Its three callback functions are executed at resolve (), Reject () and notify (), respectively,
promise.then (function (greeting) {
alert (' Success: ' + greeting);
}, function (reason) {
alert (' Failed: ' + reason);
}, Function (update) {
alert (' Got Notification: "+ update";
});
3. Promise
When a deferred instance is created, the promise instance is also created. The deferred derived promise can be retrieved by deferred.promise.
The purpose of promise is to allow interested parties to access the results of deferred task completion.
According to COMMONJS's convention, promise is an interface that interacts with an object, indicating that the result of an action is asynchronous, and that it is possible or impossible to complete at any given point in time. (This is a good word, my understanding is that promise is equivalent to a promise that you can accomplish this task at a given point in time, or it may not.) If it's done, it's equivalent to resolve, and if it's not done it's equivalent to reject. I don't know if that's the right idea. )
The Promise Method:
- Then (Successcallback, Errorcallback, nitifycallback) are promise according to Resolve/reject, or will be resolve/reject, Call Successcallback/errorcallback.
- The abbreviation for catch (errorcallback) then (null, Errorcallback).
- Finally (callback, Notifycallback)
Supplementary Note:
Promise.then () will return a new derivative promise to form a promise chain. For example:
PROMISEB = promisea.then (function (result) {return result
+ 1;
});
Promiseb'll be resolved immediately after Promisea are resolved and its value//would be the result of
Promisea in Cremented by 1
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!