8 ms·
Strict error handling in JavaScript with a functional try
- ckmar 3y agoOne line of code which is specific in what it catches: // Either foo or someError will be defined const [foo, someError] = itry(someFn, SomeError); 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. let foo; try { foo = someFn(); } catch (err) { if (err instanceof SomeError) { // do something with error } else { throw err; } } // use foo