Wraps a Promise in a try-catch block to safely execute it and returns a ResultAsync.
Promise
try-catch
The type of the success value.
A string literal type for the error label.
The promise to execute safely.
The error label to use if the promise rejects.
A ResultAsync containing either the success value or a structured error.
const [error, value] = await safeAsync(fetch('https://api.example.com/data'), 'fetch_error');if (error) { console.error(error.source);} else { console.log(value);} Copy
const [error, value] = await safeAsync(fetch('https://api.example.com/data'), 'fetch_error');if (error) { console.error(error.source);} else { console.log(value);}
Wraps a
Promise
in atry-catch
block to safely execute it and returns a ResultAsync.