1. $q
$q is a built-in service of Angular that allows you to execute functions asynchronously and allows you to use the function's return value (or exception) when the function execution is completed.
2. defer
The literal meaning of defer is delay, $() can create a deferred instance (delay object instance).
The deferred instance is intended to expose derived Promise instances, as well as signal APIs used as successful or unsuccessful completion, and the status of the current task. This sounds very complicated. The summary of the relationship between $q, defer, and promise is as follows.
var deferred = $(); //Register a delay object deferred through $q servicevar promise = ; //passdeferredDelayed object,Can get a promisepromise,andpromiseThe completion result of the current task will be returned
Defer method:
- (value) Successfully resolved its derived promise. The parameter value will be used as a parameter of the successCallback function in (successCallback(value){...}, errorCallback(reason){...}, notifyCallback(notify){...}).
- (reason) Its derived promise was not successfully resolved. The parameter reason is used to illustrate the reason for the failure. At this time, the promise object of the deferred instance will catch an error that the task was not successfully executed (errorCallback(reason){...}). To add, (errorCallback) is actually the abbreviation of (null, errorCallback).
- notify(value) Update the execution status of the promise (the translation is not good, the original words are provided updates on the status of the promise's execution)
Small examples of defer:
function asyncGreet(name) { var deferred = $(); // Create a deferred delay object through $(). When creating a deferred instance, a derived promise object will also be created, and the derived promise can be retrieved by using it. ('About to greet ' + name + '.'); //The notify method of the delay object. if (okToGreet(name)) { ('Hello, ' + name + '!'); //The task was successfully executed } else { ('Greeting ' + name + ' is not allowed.'); //The task was not successfully executed } return ; //Return the promise object of the deferred instance} function okToGreet(name) { //Just just mock data, the actual situation will be implemented according to the relevant business code if(name == 'Superman') return true; else return false; } var promise = asyncGreet('Superman'); //Get promise object//Then function of the promise object will obtain the execution status of the current task, that is, the current deferred delay instance. Its three callback functions will be executed when resolve(), reject() and notify() respectively(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. By passing, you can retrieve the deferred derived promise.
The purpose of promise is to allow interested parties to access the results of the deferred task.
According to CommonJS convention, promise is an interface that interacts with objects, indicating that the result of an action is asynchronous and may or may not be completed at any given point in time. (This sentence is so confusing. My understanding is that promise is equivalent to a promise, which promises that your task may be completed at a given time point or may not be completed. If it is completed, it is equivalent to resolve, and if it is not completed, it is equivalent to reject. I wonder if this is correct?)
Method of promise:
- Then(successCallback, errorCallback, nitifyCallback) According to the promise, it is resolved/rejected, or it is about to be resolved/rejected, successCallback/errorCallback is called.
- catch(errorCallback) then(null, errorCallback).
- finally(callback, notifyCallback)
Additional Notes:
()A new derivative will be returnedpromise,formpromisechain。For example: promiseB = (function(result) { return result + 1; }); // promiseB will be resolved immediately after promiseA is resolved and its value // will be the result of promiseA incremented by 1
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!