Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to suggest a small change. One element you weren't clear on is whether or not the treatment1 and treatment2 results are different each time. If they are then do what @raghu and @juan-garcia</p> <pre><code>function onClick() { var treatment1 =$.ajax({url: '/call1'}); var treatment2 = $.ajax({url: '/call2'}); $.when(treatment1, treatment2).done(function(obj1, obj2) { // do whatever else you need }); } </code></pre> <p>If they don't change then do this :</p> <pre><code>var treatment1 =$.ajax({url: '/call1'}); var treatment2 = $.ajax({url: '/call2'}); function onClick() { $.when(treatment1, treatment2).done(function(obj1, obj2) { // do whatever else you need }); } </code></pre> <p>Or some variation of that. Because once they are complete, your callback function will always execute right away. It's still asynchronous, but it doesn't need to wait since everything is ready to go. This serves both use cases. This is a very common pattern for data that may take a few seconds to load before it's functionally useful when drawing a new component in the page. It's a lazy-load mechanism that's very useful. Once it's in though everything looks as if it's responding instantaneously.</p> <p>I reworked the javascript in your example on JSFiddle to show just the basics I think you needed to see. That is <a href="http://jsfiddle.net/MwNN9/" rel="nofollow">here</a>. Given your example, I think the mistake is in believing that resolve must be called multiple times to trigger a behavior. Invoking the done behavior cues a one time behavior and each invocation of done loads a new behavior into the queue. Resolve is called one time. $.when().done() you call as many times as you have behaviors dependent on the specific when() condition.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload