JS Promises Exceptions Handling

Nora LC
1 min readJul 1, 2021

.catch() — Why?

source

This article is intended to clarify and highlight the basic motivations behind error catching.

Asynchronous JS operations need promises to work. And as you might already know, promises represent the eventual success or failure of that asynchronous operation along with the resulting data.

we use .then() to get the result value when the promise is of success.

On the other hand, .catch() allows us to catch the exception in the case of a rejected promise.

You might be wondering…

why bother and catch errors?

-Two (2) whys -

Catching errors and handling them is unmissable. It helps us prevent the potential crash following a promise rejection. It also provides us with some precious specific information about the bug and its source.

Meaning that in addition to building friendly user interfaces that keep the user informed about all kinds of behaviors, we also can build bug-free traceable code.

Alright 👊 — I am assuming that you are convinced.

Now that the idea is clear, I hope we won’t feel confused anymore when it comes to simply understand when to catch errors. Just remember checking if the two whys apply. And — that’s positive — in most cases.

--

--