Using $.when () to solve Ajax async puzzles: Multiple AJAX operations with logic and (and)

Source: Internet
Author: User

The previous article, "Promise solution provided by Jquery.deferred", mentions 3 questions about JavaScript asynchronous operations, as well as JavaScript Promise Getting Started. Now let's look at how to use $.when () to solve the 3rd question in the previous blog post.

" consider such a scenario. If we were to send two AJAX requests at the same time, and then to do one more thing after the two requests were successfully returned, think of the assumption that it would be very difficult to add callbacks just in the way they were called in the first place? "

Using when (), we can solve the problem with code similar to the following.

The following code can achieve this effect: when the AJAX1 and AJAX2 operations are successful. The Ondone callback function is called.

var promise1 = $.ajax (URL1), Promise2 = $.ajax (url2), promisecombined = $.when (promise1, Promise2);p Romisecombined.done ( Ondone);

The $.when () method merges multiple promise to get a new promise, which is equivalent to establishing an and (logical and) relationship between the original multiple promise. Assuming that all the constituent promise have been successful, the combined promise is also successful. Assuming that a random constituent promise fails, the merged promise fails immediately.


1.$.when () does not pass the number of references, or the number of references is neither deferred object nor promise object.

Such a scenario has little practical effect.

If A single argument was passed to Jquery.when () and it was not a Deferred or a Promise, it would be treated as a resolved De ferred and any donecallbacks attached'll be executed immediately. The donecallbacks is passed the original argument. In this case any failcallbacks might set was never called since the Deferred is never rejected. For example:

$.when ({testing:123}). Done (function (x) {  alert (x.testing);//Alerts "123" immediately});

If You don't pass it any arguments at all, Jquery.when () would return a resolved promise.
$.when (). Done (function (x) {  alert ("I fired immediately"),//x is undefined});

2.$.when () has only 1 of the parameters. The parameter is a deferred or promise object.

If A single Deferred are passed to Jquery.when (), their Promise object (a subset of the Deferred methods) is returned by the Method. Additional methods of the Promise object can be called to attach callbacks, such as Deferred.done. When the Deferred was resolved or rejected, usually by the code that created the Deferred originally, the appropriate CALLB ACKs'll be called. For example, the JQXHR object returned by Jquery.ajax () are a Promise-compatible object and can be used this-to:

$.when ($.ajax ("Test.aspx")). Done (function (data, textstatus, JQXHR) {  alert (jqxhr.status);//Alerts 200});

Create 1 lingering objects var DTD = $. Deferred ();//Returns the Promisevar its_promise = $.when (DTD) of this lingering object, or the ability to use promise to bind various callback functions Its_promise.done (function () {alert ("Success"); /change state can only be dtd.resolve () by deferred object;

The 3.$.when () parameter is a multiple deferred or promise object. Such a scenario is where the real value lies.

In the case where multiple Deferred objects is passed to Jquery.when (), the method returns the Promise from a new "master "Deferred object that tracks the aggregate state of the The deferreds it has been passed. The method would resolve its master Deferred as soon as all the deferreds resolve, or reject the master Deferred as soon as One of the deferreds is rejected. If the master Deferred is resolved, the donecallbacks for the master Deferred is executed. The arguments passed to the donecallbacks provide the resolved values for each of the deferreds, and matches the order the Deferreds were passed to Jquery.when (). For example:

var D1 = $. Deferred (); var d2 = $. Deferred (); $.when (D1, D2). Done (function (v1, v2) {    console.log (v1);//"Fish"    console.log (v2);//"Pizza"}); D1.resol ve ("Fish");d 2.resolve ("Pizza");
This is equivalent to 2 promise for logic and operation, get 1 new promise.

in the event a Deferred was resolved with no value, the Correspondin G Donecallback argument'll be undefined. If a Deferred resolved to a of single value, the corresponding argument would hold that value. In the case where a Deferred resolved to multiple values, the corresponding argument would be an array of those values. For example:

var D1 = $. Deferred (); var d2 = $. Deferred (); var d3 = $. Deferred (); $.when (D1, D2, D3). Done (function (v1, v2, v3) {console.log (v1);//V1 is undefined Console.log (v2);//V2 I S "abc" Console.log (v3); V3 is an array [1, 2, 3, 4, 5]}); D1.resolve ();d 2.resolve ("abc");d 3.resolve (1, 2, 3, 4, 5); 


uses $.when () to solve Ajax async puzzles: Multiple AJAX operations with logic and (and)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.