事件:'unhandledRejection'


\Event: 'unhandledRejection'

版本历史
版本变更
v7.0.0

不处理 Promise 拒绝已被弃用。

v6.6.0

未处理的 Promise 拒绝现在将触发进程警告。

v1.4.1

新增于: v1.4.1

  • reason <Error> | <any> Promise 被拒绝的对象(通常是 Error 对象)。

    \reason <Error> | <any> The object with which the promise was rejected (typically an Error object).

  • promise <Promise> 被拒绝的 promise。

    \promise <Promise> The rejected promise.

每当 Promise 被拒绝并且在事件循环的一个轮询内没有错误句柄附加到 promise 时,则会触发 'unhandledRejection' 事件。使用 Promises 编程时,异常被封装为 "被拒绝的 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' 事件的情况。为了解决此类故障,可以将非操作 .catch(() => { }) 句柄附加到 resource.loaded,这将阻止触发 '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.

AltStyle によって変換されたページ (->オリジナル) /