function rescue
rescue<T, F extends Any, A extends readonly unknown[]>(
fn: (...args: A) => T,
classify: (cause: unknown) => F,
...args: A
): Rescued<T, F>

Calls fn(...args) and returns the value bare, classifying any throw into the declared Failure classify builds — the boundary and the declaration fused into one expression, producing the bare union directly. The sync sibling of Task#mapErr: like that adapter edge it deliberately catches everything, because this IS the edge where a foreign throw becomes a declared failure — chain the original under cause. When the boundary should stay raw and the declaration should be a separate visible rethrow line, use Result.try instead.

import * as Result from './index.ts';
import * as Failure from './failure.ts';

const BadRecord = Failure.define('BadRecord', (d: { line: number }) => `bad record at line ${d.line}`);

Result.rescue(JSON.parse, (cause) => BadRecord({ line: 7 }, { cause }), '{"id":1}'); // { id: 1 } — bare
const failed = Result.rescue(JSON.parse, (cause) => BadRecord({ line: 8 }, { cause }), 'nope');
Failure.is(failed); // true — the throw arrived as the declared failure, bare and typed

Type Parameters

F extends Any
A extends readonly unknown[]

Parameters

fn: (...args: A) => T
classify: (cause: unknown) => F
...args: A

Return Type

Usage

import { rescue } from "lib/result/try.ts";