tryit
Wraps a function so it never throws. Instead, it returns a [error, result] tuple.
Useful for avoiding try/catch blocks and handling errors in a functional style.
Available since v2.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fn | function | The function to wrap (sync or async) |
Returns
Section titled “Returns”function — A new function that returns a Result tuple
Examples
Section titled “Examples”Safe JSON parsing
Section titled “Safe JSON parsing”Wraps JSON.parse to return a tuple instead of throwing.
Catching errors without try/catch
Section titled “Catching errors without try/catch”On error, the first element of the tuple is the Error.
