Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Deferreds can be resolved/rejected only once... However, I think the issue is how you're structuring your code...</p> <p>As long as you're initializing your deferred each time, there isn't any problem in doing this... I think the issue is this:</p> <blockquote> <p>First, i declare the deferred as global var:</p> <pre><code>var treatment1 =$.Deferred(); var treatment2 = $.Deferred(); </code></pre> </blockquote> <p>Instead, can you try doing this in a function that's invoked in the button click </p> <blockquote> <p>The user can clic a button to search for users</p> </blockquote> <p>so have a function like so:</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>Now from the rest of your post, looks like you're trying to reuse the deferreds - but in that case, your original solution should not have a problem with keeping deffereds as global since your done will be called with whatever data they were resolved with.</p> <p>Can you post some more of your code to help explain what you're trying to do.</p> <p><strong>Updated from my own comment below for elaboration</strong></p> <p>based on op's fiddle, he wants to be able to trigger dependent action multiple times. Solution is to have the dependent action create new deferreds and hook up a <code>$.when</code> to itself. See updated fiddle at <a href="http://jsfiddle.net/PLce6/15/" rel="nofollow">http://jsfiddle.net/PLce6/15/</a></p> <pre><code>// global var d1 = $.Deferred(); var d2 = $.Deferred(); var d3 = $.Deferred(); // here's the reset function resetDeferreds() { d1 = $.Deferred(); d2 = $.Deferred(); d3 = $.Deferred(); $.when(d1, d2, d3).done( function (responseData1, responseData2, responseData3) { DoGlobalTreatmentWithAllResponseData(responseData1, responseData2, responseData3); resetDeferreds(); }); // the onclick handlers function do3() { d3.resolve('do3 '); return d3; } // the top level $.when $.when(d1, d2, d3).done(function (responseData1, responseData2, responseData3) { DoGlobalTreatmentWithAllResponseData(responseData1, responseData2, responseData3); resetDeferreds(); }); </code></pre>
    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.
 

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