Skip to content
Advertisement

UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block

I am getting following error in my Node-Express App

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

To say the least, I have created a helper function which looks something like this

JavaScript

and then I am importing this helper function

JavaScript

and calling it inside my api route like this

JavaScript

From my end, I think I am handling the error by using catch block.

Question: Can someone explain me why I am getting the error and how can I fix it?

Advertisement

Answer

.catch(error => { throw error}) is a no-op. It results in unhandled rejection in route handler.

As explained in this answer, Express doesn’t support promises, all rejections should be handled manually:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement