1

I have 2 deferreds. I need to pass the return values once one is rejected to the other one.

$d1.fail(function(){
    $d2.reject.apply($d2,arguments) ;
}) ;

Is this the right way to do it?

d0001
  • 2,162
  • 3
  • 20
  • 46

1 Answers1

1

In general

Yes, but it's forward incompatible. jQuery are changing the deferred API in the next version to make their promises Promises/A+ compliant. Multiple values in deferred would no longer be supported.

So it is best to resolve your deferreds (or reject them) with a single argument for future compatibility.

That said, your code is suspicious

Usually when people have a deferred in a deferred it's because of the deferred anti-pattern. Promises chain and it is generally preferable to create new promises using .then instead of creating a new deferred explicitly. This of course also takes of error handling for you.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Can you give an example of how will this be done using then. Is it $d1.then($d2.resolve, $d2.reject) ? – d0001 Jan 30 '15 at 18:36
  • @Daniel sure thing. Does the example on the linked question on the deferred anti pattern look like your code? If it doesn't can you create a gist or fiddle showing the relevant parts of your code where `$d1` and `$d2` interact? – Benjamin Gruenbaum Jan 30 '15 at 18:38
  • @Daniel doing `$d1.then($d2.resolve, $d2.reject)` is actually another anti-pattern called the `then(success, fail)` antipattern https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-thensuccess-fail-anti-pattern – Benjamin Gruenbaum Jan 30 '15 at 18:39
  • Let me take some time to understand this anti patterns. Thanks. – d0001 Jan 30 '15 at 18:40