Member-only story
Have you ever encountered an error message like this while working with promises in JavaScript?
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: 2)
(node:5436) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
This warning is indicating an unhandled promise rejection
, meaning that there was no handler for a rejected promise. When a promise gets rejected and it's not caught, it can lead to unexpected behavior and make debugging harder. But don't worry! With some best practices and proper handling techniques, we can avoid these issues altogether.
What Causes Unhandled Promise Rejections?
An unhandled promise rejection occurs when a promise is rejected but doesn’t have any…