About the Promise mode:
- The promise mode is in one of the following three states at any time:
Incomplete (unfulfilled) completed (resolved) rejection (rejected)
The then method on the Promise object, as defined by the CommonJS promise/a Standard, is responsible for adding handler functions for the completed and rejected states. The then method returns another Promise object, which can form a "pipe" style.
About deferred:
- A popular explanation for deferred objects:
A chain-operated object that provides registration of multiple callback functions, callback queue callbacks, and conveys messages of success or failure of any asynchronous operation.
//JQuery. Deferred main treatment://It is obvious that deferred is a factory class that returns an internally constructed deferred object//tuples create three $. Callbacks objects, respectively, for success, failure, processing in three states//created a Promise object with state, always, then, and Primise methodsThe //Extension Primise object generates the final deferred object, returning the objectthe//Primise object is a restricted object, read-onlyvarDeferred = function(func) { vartuples = [//1 Action //2 Listener //3 Final Status ///The following operations will be handled around these interfaces["Resolve","Done", Jquery.callbacks ("Once Memory"),"Resolved"], ["Reject","Fail", Jquery.callbacks ("Once Memory"),"Rejected"], ["Notify","Progress", Jquery.callbacks ("Memory"]], state ="Pending",//Extended Primise ObjectPromise = {state: function() {}, always: function() {}, then: function(/ * Fndone, Fnfail, fnprogress * /) {}, Promise: function(obj) {}}, deferred = {};//define Piping style interface pipePromise.pipe = Promise.then;//Add all interfaces individually to the deferred objectJquery.each (Tuples, function(i, tuple) {deferred[tuple[0]] = function() {deferred[tuple[0] +"with"]( This= = = Deferred? Promise: This,arguments);return This; }; deferred[tuple[0] +"with"] = List.firewith; });//Turn into Promise objectPromise.promise (deferred);//If the passed parameter is a function, run directly if(func) {Func.call (deferred, deferred); }returnDeferred;}
The deferred method internally recommends 2 objects, one is an deferred external interface object, and the other is an internal promise object.
The Promise object interpretation is a restricted object, which is known as a restricted deferred object, because the returned deferred no longer have resolve (with), reject (with), and notify (with) than before These can change the deferred object state and Execute Callbacklist method, can only be then, do, Fali and so on.
Its internal through the tuples array, stores all the interface API, through the traversal of all the interfaces are hung to the internal promise and deferred objects.
The methods that define done, fail, and progress are actually the add methods in the callbacks callback function, which are saved on the queue with data outside the push.
We are actually dealing with the list of queues in callbacks by Resolve, reject, and notify.
Two questions:
1, defer delay object by resolved trigger done successful callback, call before adding done, then rely on what delay processing?
2, why the Defer.then object returned to the filtered.done of the data can be similar to the pipeline style in order to overlay the subsequent done processing?
In general, JavaScript to achieve asynchronous collection, you need to "wait", such as Defer.resolve (5) Although triggered, but the processing of done has not yet been added, we have to wait to do, then and other methods to add before the execution of the resolve, The usual usage is to use the settimeout 0,image.onerror inside the resolve to act as an asynchronous wait operation.
But jquery has cleverly bypassed this collection,
The Defer.resolve (5) method actually triggers the callback return function Firewith method, which can accept a context deferred and parameter 5
Deferred[tuple[0] + "with" [this = = deferred promise:this, arguments);
Before done | Fail | The progress methods are generated by Jquery.callbacks ("once Memory") or Jquery.callbacks ("Memory").
In fact, the callback source fire method has a memory = options.memory && data, so it is very clever to cache the value of the current parameter 5, provided to the next use, this is the then,pipe chain of data a basis, At this point, we save the memory of the value of this data.
The point is, the next Defer.done operation is also the processing of the add, the done callback function is added to the list queue, and then triggered.
With memory, if we ' re not firing then
We should call right away
} else if (memory) {
Firingstart = start;
Fire (memory);
}
Because memory in the last resolve operation, the cache of 5, so the memory of the judgment display is true, so immediately triggered the fire (memory) code, so even if the trigger sequence and add the sequential inconsistent, it will not cause errors. And jquery cleverly avoids the problem of asynchronous collection, which makes the process more reliable. The visible callback function module is customized for the deferred module.
Small experience:
- Take a hard look at it or you can read it. How to achieve asynchronous? Not with settimeout to wait for the trigger behind, but by storing the value, the next call, because the display stored values, jquery a look at the value Ah, think the previous process is right, immediately triggered.
The Deferred--promise solution for jquery