This is how functional try-catch transforms your JavaScript code
How common is this?
It’s yet another instance where we want a value that depends on whether or not there’s an exception.
Normally, you’d most likely create a mutable variable outside the scope for error-free access within and after the try-catch
.
But it doesn’t always have to be this way. Not with a functional try-catch.
A pure tryCatch()
function avoids mutable variables and encourages maintainability and predictability in our codebase.
No external states are modified — tryCatch()
encapsulates the entire error-handling logic and produces a single output.
Our catch
turns into a one-liner with no need for braces:
The tryCatch()
function
So what does this tryCatch()
function look like anyway?
From how we used it above you can already guess the definition: