method Result.prototype.orElse
Result.prototype.orElse<U, F extends BaseError>(fn: (error: E) => Result<U, F>): Result<T | U, F>

Returns this result if Ok, otherwise computes a fallback from the error.

Examples

Example 1

const result = fetchData().orElse(error => {
  console.warn("Primary failed, trying backup");
  return fetchBackup();
});

Type Parameters

F extends BaseError

Parameters

fn: (error: E) => Result<U, F>

Function to compute a fallback Result from the error

Return Type

Result<T | U, F>

This result if Ok, otherwise the computed fallback

Usage

import { Result } from "result/mod.ts";