Slide 1

Slide 1 text

2025/11/15 | JSConf.jp 2025 Pre Event Promise.try で実現する 新しいエラーハンドリング New error handling with Promise.try (C) 2025 Oishi Takanori おおいし (bicstone) 1

Slide 2

Slide 2 text

2 まとめ Promise.tryは簡潔なエラーハンドリングに不可欠 funcは同期的に実行 同期/非同期エラーを捕捉 すべての結果を単一の信頼できるPromiseに正規化 Summary Promise.try is essential for concise error handling func executes synchronously Catches both sync and async errors Normalizes all results into a single, reliable Promise

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

背景 エラーは.catch()ブロックで捕 捉しonRejectedが実行 try...catch的に振る舞う 4 Promiseでのエラーハンドリング例 Promiseにおいてエラーハンドリングは重要な要素 Background Error handling with Promises Errors caught by .catch() block, onRejected is executed Behaves like try...catch Error handling is crucial in Promises

Slide 5

Slide 5 text

5 エラーハンドリングにおける 罠 同期的なエラーが捕捉されない! .catch() は処理されず例外エ ラーとなる Promise.resolveに渡される前 にエラーが投げられるため Pitfalls Synchronous errors aren't caught! .catch() is not executed, throws uncaught error Because it occurs outside the Promise

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

7 今までの回避策 (1) new Promise で囲む 同期的に呼び出され、同期的 なエラーも捕捉 欠点: コードが冗長になる Previous workarounds (1) Wrap with new Promise Called synchronously, catches synchronous errors Drawback: verbose code

Slide 8

Slide 8 text

8 今までの回避策 (2) Promise.resolve().then()を使用 同期的なエラーも捕捉 常に非同期で実行される Previous workarounds (2) Use Promise.resolve().then() Catches synchronous errors Always executes asynchronously

Slide 9

Slide 9 text

9 今までの回避策 (3) 非同期関数を使用 async関数内の同期的なエラー も捕捉される 常に非同期で実行される Previous workarounds (3) Use async functions Synchronous errors within async functions are caught Always executes asynchronously

Slide 10

Slide 10 text

10 新たなエラーハンドリング Promise.tryを使用 同期的実行もでき、同期・非 同期両方のエラーを捕捉可能 Bluebird.js, p-tryなどのライ ブラリで提供 New error handling Use Promise.try Executes synchronously, catches both sync and async errors Provided by libraries like Bluebird.js, p-try

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

13 Promise.tryの価値 「正規化」されることに価値 同期関数・非同期関数・値を返す・Promiseを返す・ 例外を投げるなど、あらゆる可能性を吸収 常に単一の、信頼できるPromiseが保証できる Value of Promise.try Value lies in "normalization" Sync? Async? Returns value? Returns Promise? Throws exception? Handles all possible return types Always guarantees a single, reliable Promise

Slide 14

Slide 14 text

14 Promise.tryの価値 「正規化」されることに価値 常に単一の、信頼できる Promiseが保証できる 素性が知れないコールバックを 安全に組み込める Value of Promise.try Value lies in "normalization" Always guarantees a single, reliable Promise Safely handles user callbacks

Slide 15

Slide 15 text

15 Promise.tryの活用例 Go言語風の [data, error] タプル 簡潔にfuncを安全に処理可能 同期的なエラーも吸収可能 Ideas using Promise.try Go-style [data, error] tuple Safely handles func concisely Absorbs synchronous errors too

Slide 16

Slide 16 text

16 まとめ Promise.tryは簡潔なエラーハンドリングに不可欠 funcは同期的に実行 同期/非同期エラーを捕捉 すべての結果を単一の信頼できるPromiseに正規化 Summary Promise.try is essential for concise error handling func executes synchronously Catches both sync and async errors Normalizes all results into a single, reliable Promise

Slide 17

Slide 17 text

大石 貴則 (@bicstone) Slides & SNS (bicstone.me) 17 最後に TSKaigi Hokuriku 2025で登壇します! Finally Speaking at TSKaigi Hokuriku 2025!