Skip to content

True Myth / result / inspectErr

Function: inspectErr()

Call Signature

inspectErr<T, E>(fn, result): Result<T, E>

Run a side effect with the error value without modifying the Result.

This is useful for performing actions like logging, debugging, or other “side effects” external to the wrapped value. (Note: You should never mutate the value in the callback. Doing so will be extremely surprising to callers.) The function is only called if the Result is Ok, and the original Result is returned unchanged for further chaining.

NOTE

TypeScript type inference is limited with the curried form. You will need to provide explicit type parameters. Alternatively, use the non-curried form to get consistent type inference.

ts
import * as result from 'true-myth/result';

const double = (n: number) => n * 2;
const logError = (value: unknown) => console.log('Got error:', value);

// Logs: "Got error: error"
const anErr = result.err<number, string>('error');
result.inspectErr(
  logError,
  result.map(
    double,
    result.inspectErr(
      logError,
      anOk
    )
  )
);

// Does not log anything, and returns `Ok(42)`.
const anOk = result.ok<number, string>(42);
result.inspectErr(
  logError,
  result.map(
    double,
    result.inspectErr(
      logError,
      anErr
    )
  )
);

Type Parameters

T

T

The type of the wrapped value

E

E

The type of the error value

Parameters

fn

(error) => void

The function to call with the error value (only called for Err)

result

Result<T, E>

The Result to inspect

Returns

Result<T, E>

The original Result, unchanged

Call Signature

inspectErr<T, E>(fn): (result) => Result<T, E>

Run a side effect with the error value without modifying the Result.

This is useful for performing actions like logging, debugging, or other “side effects” external to the wrapped value. (Note: You should never mutate the value in the callback. Doing so will be extremely surprising to callers.) The function is only called if the Result is Ok, and the original Result is returned unchanged for further chaining.

ts
import * as result from 'true-myth/result';

const double = (n: number) => n * 2;
const logError = (value: unknown) => console.log('Got error:', value);

// Logs: "Got error: error"
const anErr = result.err<number, string>('error');
result.inspectErr(
  logError,
  result.map(
    double,
    result.inspectErr(
      logError,
      anOk
    )
  )
);

// Does not log anything, and returns `Ok(42)`.
const anOk = result.ok<number, string>(42);
result.inspectErr(
  logError,
  result.map(
    double,
    result.inspectErr(
      logError,
      anErr
    )
  )
);

Type Parameters

T

T

The type of the wrapped value

E

E

The type of the error value

Parameters

fn

(error) => void

The function to call with the error value (only called for Err)

Returns

The original Result, unchanged

(result): Result<T, E>

Parameters

result

Result<T, E>

Returns

Result<T, E>