First talk about what is promise, what is $q bar. Promise is an asynchronous processing model, with many implementations, such as the famous Kris Kwal ' s Q and the deffered of jquery.
What is promise
Previous knowledge of Ajax has been able to experience the pain of callback, synchronized code is easy to debug, but the asynchronous callback code, will let the developer into a quagmire, unable to trace, such as:
Funa (Arg1,arg2,function () {
FUNCB (arg1,arg2,function () {
FUNCC (arg1,arg2,function () {
xxxx ...
})
})
})
Nesting itself is already very difficult to understand, coupled with the time to trigger the callback, which is the equivalent of worse.
But with the promise specification, it can help developers write asynchronous code in a synchronized way, such as in Angularjs:
Deferabc.resolve (XXX)
. Then (Funcsuccess () {},funcerror () {},funcnotify () {});
When the object within the resolve succeeds, the funcsuccess is triggered and funcerror is triggered if the failure occurs. A bit like
Deferabc.resolve (function () {
sunccess:funcsuccess,
error:funcerror,
notify:funcnotify
})
More bluntly, promise is a predefined implementation of the results, if successful, XXXX, if the failure, on the XXXX, as in advance to give a number of commitments.
For example, small white in school is lazy, usually let roommates with rice, and in advance with him, if there is a leek eggs to buy this dish, or buy tomatoes scrambled eggs, no matter buy can not buy to remember to pack cigarettes.
Small white let roommates bring rice ()
. Then (leek eggs, scrambled eggs with tomatoes)
. Finally (with packs of cigarettes)
$Q Services
Q Service is a promise implementation of ANGULARJS's own encapsulation implementation, compared with Kris Kwal ' s q is much lighter.
First, introduce the $q commonly used several methods:
Defer () Creates a deferred object that can perform several common methods, such as Resolve,reject,notify
All () incoming promise array, batch execution, return a Promise Object
When () passes in an indeterminate parameter and returns a Promise object if the promise standard is met.
In promise, three states are defined: Wait state, completion state, reject state.
There are several provisions regarding state:
1 change of State is irreversible
2 waiting states can become completed or rejected
Defer () method
In $q, you can use the Resolve method to become a completion state, using the Reject method to become a deny state.
Here's a look at the simple use of $q:
where defer () is used to create a deferred object, defer.promise to return a Promise object to define the then method. The then has three parameters, namely a successful callback, a failed callback, and a state change callback.
The variable or function passed in resolve returns the result as the first then method parameter. The then method returns a Promise object, so it can be written as
Xxxx
. Then (A,B,C)
. Then (A,B,C)
. Then (A,B,C)
. catch ()
. Finally ()
Continue to talk about the above code, then...catch...finally can think of Java inside the try...catch...finally.
All () method
This all () method, you can combine multiple primise arrays into one. After all promise execution succeeds, a subsequent callback is executed. The parameters in the callback are the result of each promise execution.
This method can be used when performing certain methods in bulk.
var Funca = function () {
console.log ("Funca");
return "Hello,funa";
}
var FUNCB = function () {
console.log ("FUNCB");
return "Hello,funb";
}
$q. All ([Funca (), FUNCB ()])
. Then (function (Result) {
console.log (result);
});
Results of execution:
Funca
Funcb
Array ["Hello,funa", "Hello,funb"]
When () method
The When method can pass in a parameter, which may be a value, possibly an external object that conforms to the promise standard.
var Funca = function () {
console.log ("Funca");
return "Hello,funa";
}
$q. When (Funca ())
. Then (function (Result) {
console.log (result);
});
This method can be used when the incoming parameters are not determined.
Hello,funa
The above is the ANGULARJS in the promise---$q service information detailed introduction, follow-up continue to supplement the relevant information, thank you for your support of this site!