1. Deferred object:
It is generally declared inside a function and changes its state during operation, such as success or failure, and eventually returns the Promise object for state monitoring.
Main methods:
Deferred. Resolve (param ...) : Successful execution will trigger the done callback method for the Promise object.
Deferred. Reject (param ...) : Execution failure will trigger the fail callback method for the Promise object.
Deferred. Notify (param ...) : Executing will trigger the progress callback method of the Promise object.
Deferred. Promise (): Returns an instance of the Promise object.
Other Notes:
Use $. Deferred () can declare a Deferred object.
2. Promise object:
It can be thought of as an instance of deferred, which will trigger the corresponding callback method of the Promise object when the function state changes.
Main methods:
Promise. Done (callback): The callback method when execution succeeds.
Promise. fail (callback): The callback method when execution fails.
Promise. Progress (callback): Callback method at execution time.
Promise. always (callback): As long as the deferred state changes, the always method is triggered, similar to finally.
Promise. Then (Donecallback, Failcallback, Progresscallback): Another way to define a callback function.
Other Notes:
All methods of Promise return Promise objects, so they can be chained, such as:Promise.done (Funa). Fail (FUNB). Always (FunC)
The Ajax method of jquery Returns the Promise object by default, so you can use the various callback methods of promise directly.
3. code example:
1 //--Global variable---------------------------2 varFlaga = FLAGB = FLAGC = "";3 4 //--Fun:waita---------------------------5 varWaita =function(ms) {6 if(!ms) ms = 2000;7 vardef = $. Deferred ();8SetTimeout (function() {Flaga = "Waita execution succeeded"; Def.resolve (Flaga);}, MS);9 returndef.promise ();Ten }; One //--FUN:WAITB--------------------------- A varWAITB =function(ms) { - if(!ms) ms = 2000; - vardef = $. Deferred (); theSetTimeout (function() {FLAGB = "WAITB execution failed"; Def.reject (FLAGB);}, MS); - returndef.promise (); - }; - //--FUN:WAITC--------------------------- + varWAITC =function(ms) { - if(!ms) ms = 2000; + vardef = $. Deferred (); ASetTimeout (function() {FLAGC = "WAITC execution failed"; Def.reject (FLAGC);}, MS); at returndef.promise (); - }; - - //--Call mode one: -Waita (+). Done (function(msg) { - console.info (msg); in}). Fail (function(msg) { - console.info (msg); to}). Always (function(msg) { + console.info (msg); - }); the * //--Call mode two: $Waita (.) Then (function(msg) {Panax Notoginseng console.info (msg); -},function(msg) { the console.info (msg); + }); A the //--Call way three: +$.when (Waita ()). Then (function(msg) { - console.info (msg); $},function(msg) { $ console.info (msg); - }); - the //--Call mode four: Execute three functions sequentially -$.when (Waita (2000) . Then (WAITB). then (WAITC);Wuyi //or: the$.when (Waita ()). Then (function(msg) { - console.info (msg); WuWAITB (.) Then (function(msg) { - console.info (msg); AboutWAITC (.) Then (function(msg) { $ console.info (msg); - }); - }); - }); A + //--Call mode five: Execute three functions at a time the$.when (Waita (+), WAITB (+), WAITC). Then (function(MES) { -Console.info ("All executed successfully!") " +mes); $},function(MES) { theConsole.warn ("Not all executed successfully, where:" +mes); the});
JQuery Asynchronous callback Support-Promise, Deferred