safeTry()
safeTry<
T,E>(body):Result<T,E>
Executes a generator function that can yield error results and return a final result. This provides a way to handle errors in a more functional style.
Type Parametersโ
T: Tโ
The type of the success value
E: Eโ
The type of the error value
Parametersโ
body: () => Generator<Err<never, E>, Result<T, E>> | () => Result<T, E>โ
Generator function that yields errors and returns a final result, or a function that returns a Result directly
Returns: Result<T, E>โ
The final result from the generator or function
Sinceโ
2.0.0
Examplesโ
const result = safeTry(function* () {
const user = yield validateUser(input);
const profile = yield fetchProfile(user.id);
return ok({ user, profile });
});
const result = safeTry(() => ok(42));