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?
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.
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.