Henry Tao
Henry Tao

Reputation: 1104

What is the different between catch & fail in promisejs

I am using promisejs in my app. Promisejs is a pretty cool concept. But what is the difference between catch & fail in Promisejs?

I am still confuse. Please help! Thanks,

Upvotes: 0

Views: 955

Answers (2)

Promises have 3 possible states: 1) Pending, 2) Fulfilled and 3) Rejected. If we focus on the third state then there might be various reasons of why it was rejected and that's where "catch" and "fail" enter the game.

A promise can be rejected because an error happened, for example bad written code or syntax or it can also be rejected because it couldn't be completed but with no errors, for example the process took too long and you had a timeout. If the former happens then you can use "fail" or "catch" indistinctly, if the later happened then you should use "fail".

Generally speaking using "fail" has a wider range of action than "catch" but then it depends on what you are expecting to happen to decide which one to use.

Upvotes: 2

clay
clay

Reputation: 6017

"fail" is when a promise exits logically, or the promise chain cannot (or should not) complete. For the javascript asynchronous callbacks, this is essentially calling with an error.

"catch" is a reference to exceptions and can be caught when the a promise throws an Error, or a chain of promises throws an error. Think of it as a synchronous try/catch block (performed asynchronously), and typically has a handling method like catch (from Q)

Really, these two terms are similar in practice while inside the promise framework. When other modules start throwing Errors, then you need to be more careful (and perhaps use a "finally" method, like fin() from Q, though it looks like promisejs simply handles that with the onRejected handler).

Upvotes: 1

Related Questions