Skip to content Skip to sidebar Skip to footer

Weird Behavior With Promise Throwing "unhandled Promise Rejection" Error

When I run this code using Node, it throws an Unhandled promise rejection error in the console (even showing the error caught text first). const promise = new Promise((resolve, rej

Solution 1:

In order to be considered handled, rejected promises should be synchronously chained with then(..., ...) (2 arguments) or catch(...).

promise.then(() => console.log('ok')) is a separate promise that wasn't chained with catch(...), so rejected promise will result in unhandled rejection.

If I'm in any other page (like stackoverflow.com) it throws the exception

This isn't an exception, it doesn't prevent a script from running normally. The way unhandled rejections are treated depends on Promise implementation. Chrome implementation results in Uncaught (in promise) console error by default.

That it doesn't appear on some websites in Chrome means that a website set up unhandledrejection event handler that suppresses error output.

Post a Comment for "Weird Behavior With Promise Throwing "unhandled Promise Rejection" Error"