This presentation explains Promise.try, which was standardized in ECMAScript 2025.
In JavaScript Promises, while asynchronous errors can be caught with .catch(), synchronous errors thrown before the Promise is created cannot be caught. Previously, workarounds included wrapping with new Promise, using Promise.resolve().then(), or using async functions, but these approaches had drawbacks such as verbose code or always executing asynchronously.
Promise.try executes a function synchronously while catching both synchronous and asynchronous errors, and normalizes all possible return values (values, Promises, exceptions) into a single, reliable Promise.
ECMAScript 2025で標準化された Promise.try について解説しています。
JavaScriptのPromiseでは、非同期エラーは.catch()で捕捉できますが、同期的に投げられるエラーは捕捉できないという問題がありました。従来はnew Promiseで囲む、Promise.resolve().then()を使う、async関数を使うなどの回避策がありましたが、いずれもコードが冗長になったり、常に非同期実行されるという欠点がありました。
Promise.tryは、関数を同期的に実行しつつ、同期・非同期両方のエラーを捕捉し、あらゆる戻り値(値、Promise、例外)を単一の信頼できるPromiseに「正規化」します。
【この資料で発表したイベント】
【著者について】
- Portfolio: bicstone.me
- X: @bicstone_me
© 2025 Oishi Takanori