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));