Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Promise.tryで実現する新しいエラーハンドリング New error handling...

Avatar for おおいし おおいし
November 14, 2025

Promise.tryで実現する新しいエラーハンドリング New error handling with Promise try

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に「正規化」します。

【この資料で発表したイベント】

- JSConf.jp 2025 Pre Event

【著者について】

- Portfolio: bicstone.me
- X: @bicstone_me

© 2025 Oishi Takanori

Avatar for おおいし

おおいし

November 14, 2025
Tweet

More Decks by おおいし

Other Decks in Programming

Transcript

  1. 2025/11/15 | JSConf.jp 2025 Pre Event Promise.try で実現する 新しいエラーハンドリング New

    error handling with Promise.try (C) 2025 Oishi Takanori おおいし (bicstone) 1
  2. 4年以上一貫してVertical SaaSの開発に 携わるWebエンジニア 一番好きなES2025の新機能は: “New Set methods” 3 自己紹介 おおいし

    (bicstone) About me Web developer with 4+ years in SaaS development Favorite ES2025 feature: "New Set methods" Certified ScrumMaster® is a certification mark of Scrum Alliance, Inc. Any unauthorized use is strictly prohibited.
  3. 6 エラーが捕捉されない理由 次のように評価されるため 1. nullを評価 2. func(null)を同期的呼び出し 3. throw new

    Error(...) を評価 4. 例外がスローされ、実行停止 Why errors aren't caught Evaluated as follows: Evaluate null Call func(null) synchronously throw new Error(...) executes Exception thrown, execution stops
  4. 7 今までの回避策 (1) new Promise で囲む 同期的に呼び出され、同期的 なエラーも捕捉 欠点: コードが冗長になる

    Previous workarounds (1) Wrap with new Promise Called synchronously, catches synchronous errors Drawback: verbose code
  5. 11 新たなエラーハンドリング Promise.tryの標準化 ECMAScript 2025で標準化 Baseline Newly Available! New error

    handling Standardization of Promise.try Standardized in ECMAScript 2025 "Promise.try()" by Mozilla Contributors, licensed under CC-BY-SA 2.5.
  6. 12 新たなエラーハンドリング Promise.tryの動作 funcを同期的に呼び出し返値が 値: fulfilledのPromise 例外: rejectedのPromise Promise: 状態を引き継ぐ

    New error handling How Promise.try works Calls func synchronously, return value: Value: fulfilled Promise Exception: rejected Promise Promise: inherits promise state
  7. 15 Promise.tryの活用例 Go言語風の [data, error] タプル 簡潔にfuncを安全に処理可能 同期的なエラーも吸収可能 Ideas using

    Promise.try Go-style [data, error] tuple Safely handles func concisely Absorbs synchronous errors too
  8. 大石 貴則 (@bicstone) Slides & SNS (bicstone.me) 17 最後に TSKaigi

    Hokuriku 2025で登壇します! Finally Speaking at TSKaigi Hokuriku 2025!