Home >Web Front-end >JS Tutorial >jQuery source code analysis notes (3) Deferred mechanism_jquery

jQuery source code analysis notes (3) Deferred mechanism_jquery

WBOY
WBOYOriginal
2016-05-16 18:05:55957browse

Deferred registers callback functions into a queue, manages them uniformly, and can call these functions synchronously or asynchronously.
jQuery.Deferred() is used to construct a Deferred object. This object has state values, there are three types: Rejected, Resolved and initial state. Resolved means that the operation was successfully completed, while Rejected means that an error occurred and the call failed. The main members of the Deferred object are as follows:
done(callback): Register a callback function, which is called when the status is resolved. * fail(callback): Register a callback function, which will be called when the status is rejected. * always(callback): Register a callback function, which will be called whether it is resolved or rejected. * then(successCallback, failureCallback): Pass in the success and failure callback functions at the same time. * pipe(successFilter, failureFilter): Call the function specified by pipe before calling the success and failure callback functions. It is regarded as a pipeline mechanism that intercepts function calls. * resolve(args): Set the status to Resolved. * reject(args): Set the status to Rejected. * promise(): Returns an incomplete Deferred interface without resolve and reject. That is, the state of the Deferred object cannot be modified. Can be thought of as a read-only view. This is to prevent external functions from triggering the callback function early. For example, $.ajax no longer returns XMLHttpRequest after version 1.5, but instead returns an object that encapsulates the XMLHttpRequest and Deferred object interfaces. The Deferred part is obtained by promise(), which prevents external functions from calling resolve and reject, and prevents the callback function from being triggered before ajax is completed. Reserve the calling permission of these two functions to ajax internally.
The code for this module starts at line 939, immediately following the declaration of the jQuery object. It can also be regarded as a basic core code. It is also one of the biggest changes in version 1.5.
In fact, the code logic of Resolve and Reject is the same, but the corresponding states are different. For code reuse, a Deferred is first implemented internally, and then two Deferreds are new inside the real Deferred, one as Resolve and the other as Reject.
_Deferred object maintains a function array (callback list) internally. The job of Done(f1, f2...) is to push these callbacks to this queue in sequence and save them. And resolveWith (resolve with parameters) and resolve call this callback function in sequence.
In Done, it is necessary to determine whether the event has been completed. If the event has been completed when the callback is added to the chain, the callback needs to be executed immediately. This feature is the reason why callback no longer needs to be written together with the declaration of triggering asynchronous events. For example, it turns out that $.post("...", function(data) { ... }) must be written. This success callback must be written here, but now it can be written:

Copy code The code is as follows:

var defer = $.post("...");
// ...
defer.success(function(data) {
// ...
});
// ...
defer.fail(function(data) {
// ...
});

Such asynchronous event declaration and callback function They can be managed separately. This is the biggest change after the rewrite in version 1.5.
The pipe(successFilter, failureFilter) function modifies the callback list in the original object. Filter functions are inserted in front of the two callback lists using the then function. Then return. In this way, when the state of this Deferred object changes, the Filter function specified by the pipe function will be called first, and then the callback list will be called.
promise() is much simpler, it just creates a new object and then copies the required members into it. The required members are defined in a constant called promiseMethods.
Copy code The code is as follows:

var promiseMethods = "done fail isResolved isRejected promise then always pipe ".split(" ");
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn