0

I am using jquery for promises.

I have a scenario, where i have two promises.

If promise1 rejects or resolve then system1 should reject or resolve respectively.

If promise2 rejects or resolve, system2 should resolve.

Function X should be called after both are settled and both system1 and system2 has resolved.

I tried :

var dp = $.when(promise1, promise2);
dp.done(function(one,two){
     X();
}).fail(function(){
  // promise1 might have not settled as of yet.
    Should call X or not ?
});

But it returns as soon as one of the promise fails. So my promise1 is not resolved at the time when my fail is called.

How do i do it ?

Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
  • You want both of the promises to run what ever is the result and after both of their completion you want to run the X() method. Is it what you want? – Shaik Mahaboob Basha Feb 14 '14 at 07:04
  • @Basha Please see the edited question. I actualy want X to be called iff promise1 is resolved but only after both promises have settled. – Ashish Negi Feb 14 '14 at 07:53

1 Answers1

0

The behaviour is exactly as per the documentation: The answer for your question is also in the documentation as italised.

In the multiple-Deferreds case where one of the Deferreds is rejected, jQuery.when immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. If you need to perform additional processing for this case, such as canceling any unfinished ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.

jacquard
  • 1,307
  • 1
  • 11
  • 16
  • I tried "when" for my solution, but could not apply it. May be it is wrong. I am new to promises. "when" is behaving right and i am not contradicting it. i wanted to know how do i solve my problem (with or without when)? – Ashish Negi Feb 14 '14 at 06:52
  • The [fiddle](http://jsfiddle.net/rD4bE/) gives an example.. You could call `req2.abort()` in your `fail` callback. – jacquard Feb 14 '14 at 07:23