guard
Wraps a function so that if it throws, a default value is returned instead of propagating the error. Works with both synchronous and asynchronous functions.
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 guard |
defaultValue | T | The value to return if the function throws |
Returns
Section titled “Returns”Promise<T> — The result of the function, or the default value on error
Examples
Section titled “Examples”Fallback on parse error
Section titled “Fallback on parse error”Returns a default value when the function throws.
Pass-through on success
Section titled “Pass-through on success”Returns the function result when it does not throw.
