WorkerGlobalScope: rejectionhandled イベント

rejectionhandled イベントは、スクリプトのグローバルスコープ(通常は WorkerGlobalScope)に送られるものであり、拒否された Promise が遅れて処理されるたび、すなわち、拒否されたプロミスが unhandledrejection イベントが発生した後にハンドラーが割り当てられた場合に送られます。

これは、プロミスが拒否され、その時刻に拒否ハンドラーがない場合に送られる unhandledrejection イベントと一緒に、デバッグや一般的なアプリケーションの回復のために使用することができます。

構文

このイベント名を addEventListener() などのメソッドで使用するか、イベントハンドラープロパティを設定するかしてください。

js
self.addEventListener("rejectionhandled", (event) => {});
self.onrejectionhandled = (event) => {};

イベント型

PromiseRejectionEvent です。 Event から継承しています。

Event PromiseRejectionEvent

イベントプロパティ

PromiseRejectionEvent.promise 読取専用

拒否されたプロミス (Promise) です。

PromiseRejectionEvent.reason 読取専用

プロミスが拒否された理由を示す値または Object で、Promise.reject() に渡されたものです。

rejectionhandled イベントを使用することで、拒否されたプロミスを、拒否された理由とともにコンソールにログ出力することができます。

js
self.addEventListener("rejectionhandled", (event) => {
  console.log(`Promise rejected; reason: ${event.reason}`);
});

仕様書

No specification found

No specification data found for api.WorkerGlobalScope.rejectionhandled_event.
Check for problems with this page or contribute a missing spec_url to mdn/browser-compat-data. Also make sure the specification is included in w3c/browser-specs.

ブラウザーの互換性

BCD tables only load in the browser

関連情報