Executes a function and returns Some with the result if successful, or None if it throws.
Some
None
This converts try-catch patterns to Option-based handling, where you only care about success/failure, not the error details.
Similar to Promise.try, this function accepts optional arguments that are passed to the function.
Promise.try
The type of the value returned by the function.
The types of the arguments to pass to the function.
A function that may throw an exception.
Arguments to pass to the function.
Some<T> if the function succeeds, or None if it throws.
Some<T>
1.7.0
// Parse JSON, ignore error detailsconst data = tryOption(JSON.parse, jsonString);console.log(data.unwrapOr(defaultData)); Copy
// Parse JSON, ignore error detailsconst data = tryOption(JSON.parse, jsonString);console.log(data.unwrapOr(defaultData));
// Validate URL - using closure formconst url = tryOption(() => new URL(input));url.inspect(u => console.log('Valid URL:', u.href)); Copy
// Validate URL - using closure formconst url = tryOption(() => new URL(input));url.inspect(u => console.log('Valid URL:', u.href));
// Decode URI component with argumentsconst decoded = tryOption(decodeURIComponent, str); Copy
// Decode URI component with argumentsconst decoded = tryOption(decodeURIComponent, str);
Executes a function and returns
Somewith the result if successful, orNoneif it throws.This converts try-catch patterns to Option-based handling, where you only care about success/failure, not the error details.
Similar to
Promise.try, this function accepts optional arguments that are passed to the function.