TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Strict error handling in JavaScript with a functional try

1 点作者 ckmar将近 2 年前

1 comment

ckmar将近 2 年前
One line of code which is specific in what it catches:<p><pre><code> &#x2F;&#x2F; Either foo or someError will be defined const [foo, someError] = itry(someFn, SomeError); </code></pre> Instead of nine lines which feel like they are working uphill against the language: dealing with variable scoping issues, deeper nesting, and footguns like forgetting to re-throw.<p><pre><code> let foo; try { foo = someFn(); } catch (err) { if (err instanceof SomeError) { &#x2F;&#x2F; do something with error } else { throw err; } } &#x2F;&#x2F; use foo</code></pre>