I have two handlers: onSuccess and onFailure, but I want sometimes (when the server decides so), to prevent either of them from running. I know I can return some 'ignore' from the .pipe()
and to test for it within the .done()
and .fail()
handlers, but is there more elegant way, like tweak something in the this
inside the .pipe()
?
Asked
Active
Viewed 172 times
0

Tar
- 8,529
- 9
- 56
- 127
1 Answers
1
Could you provide more details of your case ?
When does onFailure
should be called or not ? If your server call fails, how could you be able to know if you need to call something or nothing as you ask ?
If I understand what you ask, you want to avoid to have a "if" statement in all your methods onSuccess
and onFailure
. A way to do that could be to add an intermediate .pipe
call and returning a new promise depending on your server answer to do something or not.

Mordhak
- 2,646
- 2
- 20
- 14
-
do you mean return `$.Deferred()` in `.pipe()` ? like: `def.pipe(...){ if (break_the_chain) return $.Deferred(); }` ? – Tar Jun 07 '12 at 10:45
-
Yes, something like that. The problem with this solution is if your server call fails, you can't switch server answer, or do it in the "fail" of your first deferred. I need more information about your "switch" conditions or server call to give you better code example. – Mordhak Jun 07 '12 at 12:11
-
That's my current solution - return `$.Deferred()` and do nothing with it. Hope there's no memory leak or something (since these returned deferred are never resolved/rejected..) – Tar Jun 07 '12 at 12:31