TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Strict error handling in JavaScript with a functional try

1 pointsby ckmaralmost 2 years ago

1 comment

ckmaralmost 2 years ago
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>