Wrap a throwing function
When you need to call a function that can throw — from a third-party library, a built-in API, or legacy code — wrap the call in Result.try to capture thrown errors as typed Err values.
Synchronous
import { Result } from "antithrow";
const parsed = Result.try(() => JSON.parse(input));
if (parsed.isOk()) {
use(parsed.value);
}
parsed is Result<unknown, unknown>. Supply a type argument to narrow:
const parsed = Result.try<unknown, SyntaxError>(() => JSON.parse(input));
Asynchronous
Passing an async function (or one that returns a promise) produces a Pending:
const value = await Result.try(async () => expensive());
if (value.isOk()) use(value.value);
The await settles the Pending to Ok or Err. If you need eager failure typing, use Result.fromPromise instead (see: Convert a Promise).
Narrowing the error type
Result.try catches anything, so E defaults to unknown. Use .mapErr when you know the shape:
const parsed = Result.try(() => JSON.parse(input)).mapErr((cause) => {
return cause instanceof SyntaxError
? { kind: "invalid-json" as const, cause }
: { kind: "unknown" as const, cause };
});
See also
- Reference:
Result.try - Reference:
Result.fromPromise - How-to: Migrate from throwing code