Note that there are some explanatory texts on larger screens.

plurals
  1. POReuse Deferred more than once
    text
    copied!<p>I'm using deferred as I need to execute several processes asynchronously.</p> <p>To be clearer, here is the signification of my treatments :</p> <ul> <li>Treatment1 : call of an ajax service providing user rights</li> <li>Treatment2 : call of an ajax service providing links and labels.</li> </ul> <p>I need to call these 2 services at the same time and then get the unified response of both services in order to display links depending on rights (my real problem is with a 3rd ajax service but let's talk about with only 2 to simplify).</p> <p>First, I declare the deferred as global var :</p> <pre><code>var treatment1 = $.Deferred(); var treatment2 = $.Deferred(); </code></pre> <p>Then, when I need to do the job, I call the resolve method with needed data for using it in the global unique treatment:</p> <ul> <li>when my 1st ajax service responds : <code>treatment1.resolve(responseData1)</code></li> <li>when my 2nd ajax service responds : <code>treatment2.resolve(responseData2)</code></li> </ul> <p>When the treatment1 &amp; 2 are finished, the done event is fired :</p> <pre><code>$.when(treatment1, treatment2).done(function(responseData1,responseData2) { DoGlobalTreatmentWithAllResponseData(responseData1,responseData2) } </code></pre> <p>My problem is that deferred works only once. </p> <p>As my website is realized in ajax mainly, I need to fire the event multiple times.</p> <p>The user can click a button to search for users. Then a list of users is displayed and the ajax services are all called asynchronously. This operation can be repeated infinitely.</p> <p>I just need a way to reuse the principle of deferred but multiple times. I know that this problem has already been discussed and everyone says deferred can't work this way.</p> <p>But, is it really not possible to reset the deferred state or reset the promises (even by implementing a custom solution, using AOP or something else)?</p> <p>If it's impossible, what solution could I use? I don't want to fire treatments one after another but I really want to do a global treatment after all the treatments are finished (that is to say, after the last treatment in activity is finished) and I want to use the <code>responseData</code> of each services.</p> <p>Here is my sample code that I would like to customize : <a href="http://jsfiddle.net/PLce6/14/" rel="nofollow">http://jsfiddle.net/PLce6/14/</a></p> <p>I hope to be clear as English is not my native language.</p> <p>Thank you in advance for your help.</p>
 

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