Let's learn more about the promise asynchronous programming model by explaining the $q API. $q is a service for the Angularjs, but a simplified version of the Promise asynchronous programming model, the source code to remove the comments to implement the codes are more than 200 lines, the following begins to introduce $q API.
The defer object (lingering object) can be obtained through $q.defer (), the following is the API for the defer object:
Method:
Resolve (value): sends a message to the Promise object asynchronous execution body tell him I have successfully completed the task , value is the message sent.
reject (value): sends a message to the Promise object asynchronous execution body tell him I can't finish this task , value is the message sent.
Notify (value): sends a message to the Promise object asynchronous execution body tell him what I'm doing now , value is the message sent.
After these messages are sent , promise calls the existing callback function.
Properties:
Promise is the committed object with this defer object.
From the above can be seen defer is mainly used to send messages.
Promise The object can be obtained through defer.promise, the following is the API for the Promise object:
Method:
Then (successcallback,errorcallback,notifycallback): parameters are different callback functions under different messages, defer send different messages to execute different callback functions, Messages are passed as arguments to these callback functions. The return value is back to a promise object that exists to support chained calls. When the first defer object sends a message, the subsequent promise corresponding defer object also sends the message, but the message is sent differently, regardless of whether the first defer object is sending reject or resolve, and the second and later are sent resolve, The message is transitive.
catch (errorcallback):then (null,errorcallback) abbreviation.
finally (callback): equivalent to then (callback,callback) abbreviation, the method in this finally does not accept parameters, but can defer sent message and message type successfully passed to the next then.
Method:
defer (): used to generate a deferred object var defer = $q. Defer ();
reject (): The parameter receives an error message, which is equivalent to throwing an exception in the callback function and then invoking the wrong callback function in the next then.
All () : The parameter is received as a promise array, and a new single Promise object is returned, and when these promise objects correspond to the defer object, the single Promise object is resolved, When one of these promise objects is reject, the single promise is also reject.
When (): receives the first parameter as an arbitrary value or a Promise object, and the other 3 promise then methods, the return value is a Promise object. If the first parameter is not a Promise object, the success callback is run directly and the message is the object, and if promise then the promise returned is a wrapper over the parameter of the promise type. The message sent by the promise corresponding to the defer will be received by the Promise object returned by our when function.
API details for $Q service