Why is it a mistake to ignore the rejected promise in Chrome?

I don’t want to do anything if the promise is rejected, for example getPromise().then(foo=>{}); . Why is this a bug in Chrome?

 (new Promise((resolve, reject)=>{reject()})) Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: undefined} VM3250:2 Uncaught (in promise) undefined 

In Node and Firefox, it is normal to ignore the rejected part.

+5
source share
3 answers

Without a promise rejection handler, chrome will asynchronously generate a rejection reason that does not affect any JavaScript process, just prints the console reason. If you get annoyed with this, you need to add a handler, for example. Promise.reject(reason).catch(reason => {}) .

UPDATE: Why is the error occurring? This may be because a failure is usually considered an exception (error).

PS Your "question" looks like feedback, not a question. I personally prefer to consider the reason as a mistake.

+3
source

Rejecting promises is like an uncaught exception. if you want to ignore the exception - understand it, but do not handle it, the same thing here - add the .catch operator, but do nothing

Promise.reject(new Error('bad..')).catch(e => {})

I would, however, not recommend that as promises be rejected for any reason, so you might want to add some sort of processing logic

+2
source

Rejecting a promise basically means that something bad has happened and that you must handle it with a catch call. If you reject a promise without catch , it will throw an exception (more precisely, an unhandled reject)

 var a = (new Promise((resolve, reject) => { reject("my error") })).catch((err) => { console.error(err) }) 

I assume this is V8 specific if this only happens in Chrome, but it makes sense to me

+1
source

Source: https://habr.com/ru/post/1246767/


All Articles