Our common asynchronous operations are:
Timer setTimeoutpostmessageWebWorkorCSS3 Animation XMLHttpRequestHTML5 Local data, etc...
JavaScript requires asynchronous communication when interacting with the server, just like Ajax. Because it is an asynchronous model, it is similar to Ajax when invoking the local data interface provided by the Transaction Navigator (here I assume), the browser itself has internal XHR method asynchronous processing, but at this time the JS code will be synchronous down execution, in fact, is a non-blocking code.
problem: because there is no blocking, the code will continue to execute after sending the AJAX request, then the subsequent operation if the data will be dependent on the error, so this will need to wait for the Ajax to return to perform subsequent operations.
Deferred
Deferred provides an abstract non-blocking solution (such as a response to an asynchronous request), and he creates a promise object that returns a response at some point in the future, simply as a processing scheme for an asynchronous/synchronous callback function.
$. Deferred is used within the jquery code with four modules, namely the Promise method, the DOM Ready,ajax module, and the animation module.
The transformation of Ajax
Traditional jquery's Ajax operations are traditionally written (before 1.5):
$.ajax ({
URL: "",
Success:function () {alert ("Success")},
Error:function () {alert ("fail")}
});
$.ajax () accepts an object parameter, which contains two methods, the Success method, which specifies the callback function after the operation succeeds, and the error method specifies the callback function after the operation failed.
Introduced with new deferred after version 1.5
$.ajax (""). Done (function () {alert ("Success")}). Fail (function () {alert ("fail")});
Javascipt Source Parsing asynchronous