事件:'unhandledRejection'
【Event: 'unhandledRejection'】
'unhandledRejection' 事件会在每当一个 Promise 被拒绝且在事件循环的一轮内没有为该 Promise 附加错误处理程序时触发。在使用 Promise 编程时,异常被封装为“被拒绝的 Promise”。拒绝可以通过 promise.catch() 捕获和处理,并沿着 Promise 链进行传播。'unhandledRejection' 事件对于检测和跟踪那些已被拒绝但拒绝还未被处理的 Promise 非常有用。
【The 'unhandledRejection' event is emitted whenever a Promise is rejected and
no error handler is attached to the promise within a turn of the event loop.
When programming with Promises, exceptions are encapsulated as "rejected
promises". Rejections can be caught and handled using promise.catch() and
are propagated through a Promise chain. The 'unhandledRejection' event is
useful for detecting and keeping track of promises that were rejected whose
rejections have not yet been handled.】
import process from 'node:process';
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
// Application specific logging, throwing an error, or other logic here
});
somePromise.then((res) => {
return reportToUser(JSON.pasre(res)); // Note the typo (`pasre`)
}); // No `.catch()` or `.then()`const process = require('node:process');
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
// Application specific logging, throwing an error, or other logic here
});
somePromise.then((res) => {
return reportToUser(JSON.pasre(res)); // Note the typo (`pasre`)
}); // No `.catch()` or `.then()`以下情况也会触发 'unhandledRejection' 事件的触发:
【The following will also trigger the 'unhandledRejection' event to be
emitted:】
import process from 'node:process';
function SomeResource() {
// Initially set the loaded status to a rejected promise
this.loaded = Promise.reject(new Error('Resource not yet loaded!'));
}
const resource = new SomeResource();
// no .catch or .then on resource.loaded for at least a turnconst process = require('node:process');
function SomeResource() {
// Initially set the loaded status to a rejected promise
this.loaded = Promise.reject(new Error('Resource not yet loaded!'));
}
const resource = new SomeResource();
// no .catch or .then on resource.loaded for at least a turn在这个示例中,可以将拒绝情况作为开发者错误进行跟踪,就像其他 'unhandledRejection' 事件通常的处理方式一样。为了应对此类失败,可以在 resource.loaded 上附加一个非操作性的 .catch(() => { }) 处理程序,这将阻止 'unhandledRejection' 事件的触发。
【In this example case, it is possible to track the rejection as a developer error
as would typically be the case for other 'unhandledRejection' events. To
address such failures, a non-operational
.catch(() => { }) handler may be attached to
resource.loaded, which would prevent the 'unhandledRejection' event from
being emitted.】
如果 'unhandledRejection' 事件被触发但未处理,它将作为未捕获的异常抛出。除此之外,'unhandledRejection' 事件的其他行为可以通过 --unhandled-rejections 标志进行更改。
【If an 'unhandledRejection' event is emitted but not handled it will
be raised as an uncaught exception. This alongside other behaviors of
'unhandledRejection' events can changed via the --unhandled-rejections flag.】