Why I get the "(node:7424) UnhandledPromiseRejectionWarning" message for handled error?

Andrey Bushman

I handle the error in my Promise by catch, but in Node.js (v10.11.0) console output I see the message: (node:7424) UnhandledPromiseRejectionWarning: Error: Oops...

Why does it happen?

// See browser console, not stack snippet console, to see uncaught error

const p = new Promise((resolve,reject) => { throw new Error('Oops..');});

p.then(()=> console.log('Then'));
p.catch(err => console.log(err.message)); // the error handling

console.log('The end');

Also, I get the same result for such variant of p initializing:

const p = new Promise((resolve,reject) => { reject(new Error('Oops..'));});

This is my output:

The end
Oops..
(node:7492) UnhandledPromiseRejectionWarning: Error: Oops..

CertainPerformance

Whenever you call .then (or .catch) on an existing Promise, you have a new Promise. The error results from the Promise chain created by

p.then(()=> console.log('Then'));

not being caught anywhere.

Either chain the .catch onto the .then:

const p = new Promise((resolve, reject) => {
  throw new Error('Oops..');
});

p
  .then(() => console.log('Then'))
  .catch(err => console.log(err.message));

console.log('The end');

Note that when constructing a Promise, it's a good idea to always call reject explicitly when there's an error, to ensure that the consumer of that Promise can catch problems. For example, in the following code, the p will not be rejected, and will remain unresolved forever, because the error was thrown asynchronously:

const p = new Promise((resolve, reject) => {
  setTimeout(() => {
    throw new Error('Oops..');
  });
})

p
  .then(() => console.log('Then'))
  .catch(err => console.log(err.message))

console.log('The end');

Better to call reject:

const p = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject('Oops..');
  });
})

p
  .then(() => console.log('Then'))
  .catch(err => console.log(err))

console.log('The end');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get errors which are handled serverside in a ajax error message after submitting a form?

Why I get error message that onVideoSelect is not a function?

Why does the constructor handle the exception but I get a "not handled exception error" when I use it

How to get the error message in Controller or Route after handled exception in errorHandler?

Why do I get the "setShowModal is not defined" error message in react?

Why do I get a "No test adapters are referenced by this solution" error message?

Why I get error message when transfer INSERT method to Function

Why I get error message "Invalid number" since UserID is NUMBER

Why I get an error message "Notice: Undefined index: formats ..." in php?

Why do i get the error message for a bool in a if statement

Why do I get 'int is not subscriptable' error message?

Why do I get an error message in Laravel, but the request is still running?

Why does audio.buffered.end(0) get an error message when I try to get buffered time

Why do I get an error message using "sudo apt-get update"?

Why do I get an error message of NoneType object is not callable when I try to call this function?

Python/Terminal - Why am I getting this error message when I try to get user input

I get an error message with type match error

Why cant i show error message?

Why do I get the error message "http: response.Write on hijacked connection"?

Why do I get an error message when trying to assign a rect to a surface?

Why I get Value cannot be null. (Parameter 'connectionString') error message after Adding Migrations

Why do I get the error message "undefined is not a function" when trying to cancel a promise on press?

Why do I get an error message when installing Microsoft.EntityFrameworkCore through NuGet

Can you tell me why i get the Error Message invalid syntax on my command

Why do I get an error message plotting indicating infinite values when there are none?

Why do I get an error message pointing to Inf values when trying to plot counts over time in R?

Why I get the error message that my custom logging handler "has no attribute `level`"?

Why do I get error message Cast to ObjectId failed for value \"[Function: ObjectId]"?

Can anyone tell me why I get this 'ERROR: Job failed: exit code 1' message from GitLab?