Before looking at someone else's demo, it is found that the code to be executed when the delay object is resolve is sometimes written in the Deferred.then method and sometimes written in the Deferred.done method. This makes me very confused about the lingering object smattering, today I took the time to study the next, and found that: in some environment, two methods do achieve the same effect. What about this particular environment? Let's take a look at the usage of Deferred.done:
Create deferred object var DTD = $. Deferred ();//Solve Deferred object Dtd.resolve (' Finish ');//Call Done method Dtd.done (Donecallback [, Donecallback])//When Deferred object is Resolve, the Execute Donecallback function//parameter can be a function, multiple functions or an array of functions//return the original deferred or Promise object
Then look at the usage and characteristics of Deferred.then:
Create deferred object var DTD = $. Deferred ();//Solve Deferred object Dtd.resolve (' Finish ');//Call then method Deferred.then (Donefilter [, Failfilter] [, Progressfilter ]/Then Method attribute://When the deferred object is resolve, execute the Donefilter function//When the deferred object is reject, execute failfilter function//When dederred object is progress, Executes the Progressfilter function//return value: 1, returns the Promise object of the deferred, modifies the value passed by Promise (the original Resolve,reject return value is a, change A to B, return B, The return value received by the promise's done or fail is changed to B);//return value: 2, create a new deferred object within the then method and return its promise//the Promise object returned can link other lingering objects, such as Done,fail, Then wait//multiple then method, asynchronous execution (one by one)//the method will filter out the method of deferred modify state, return value deferred object promise
Based on the characteristics of the above two methods, it is found that:
Both the Deferred.then and Deferred.done methods can receive a parameter function directly, and the first parameter function is called when the deferred object is resolve.
Although the then method can change the return value, the two methods do achieve the same effect without considering the return value and having only one parameter function.
By contrast, the done method is more pure, then the then method will be more complex, but not completely replace the Do method, using the then method, it is better to be careful.
Additional methods for attaching deferred objects:
var DTD = $. Deferred ();
var state = Dtd.state ();//Returns the current status of the deferred object, pending/resolved/rejected//does not accept any parameters
Deferred.always (Alwayscallback [, Alwayscallback]);//When the deferred object is resolved or rejected, this method is executed//The argument can be a function, or an array of functions
Dtd.promise ([obj]);//Purpose: Prevents other code from interfering with its internal progress and state//Returns a new Promise object containing the methods that can be executed (done, fail, then, always, progress, states, promise ),//does not contain a method to modify the deferred state (resolve, Reject, notify, Resolvewith, Rejectwith, Nodifywith)//To return the deferred object, Recommended return Deferred.promise ()
Dtd.resolve ([args])//Resolve deferred object, call all Donecallback functions//Donecallback can be set through the first parameter in the then method, or through Dtd.done (donecallback) The Add//parameter is passed to Donecallback. Parameters are optional//only the creator of the deferred object can call the method//Donecallback in this is the deferred or Promise object//Donecallback only receive one parameter
Dtd.resolvewith (context [, args])//Resolve deferred object, call all Donecallback functions//Parameters: The first argument is the context is the this object, and the Donecallback this will be modified The second parameter is an array//Donecallback in the context of the call Resolvewith method//Donecallback receives the number of parameters for the method the length of the second parameter arrays//differs from the Resolve method in that Change the Donecallback function to this point
Dtd.reject ([args])//Reject deferred object, call all Failcallback functions//Failcallback can be set through the second parameter in the then method, or through Dtd.fail (failcallback) The Add//parameter is passed to Failcallback. Parameters are optional//only the creator of the deferred object can call the method//Failcallback in this is the deferred or Promise object//Failcallback only receive one parameter
Dtd.rejectwith (context, [args])//Resolve deferred object, call all Failcallback functions//Parameters: The first argument is the context, which is the This object, The Failcallback this will be modified, the second parameter is an array//Failcallback in the context of the call Rejectwith method//Failcallback receives the number of parameters for the method the length of the second parameter arrays// The difference from the Resolve method is that the this point of the Failcallback function will be changed
Dtd.notify ([args])//deferred when processing, call all Progresscallback functions//Progresscallback can be set through the then method of the 3rd parameter, can also be added by Deferred.progress (progresscallback)//Usually this method can only be called by the creator of the deferred object, which can be filtered by deferred.promise or then//parameter is not writable. If a parameter is written, it is suggested that a string or a function that can return a string//when deferred enters the resolved or rejected state, and then calls the Notify method, Progresscallback will no longer be executed
Dtd.notifywith (context, [args])//Deferred call all Progresscallback functions//Parameters when processing: The first argument is the context, which is the This object, The Progresscallback this will be modified, the second parameter is an array//Progresscallback in the context of the call Rejectwith method// Progresscallback receives the number of parameters for the method the length of the second parameter array//differs from the Resolve method in that it will change the Progresscallback function of this point//when deferred enters resolved or rejected state, and then call the Notifywith method, Progresscallback will no longer be executed
Lingering objects for jquery