Skip to content

True Myth / result / inspect

Function: inspect() ​

Call Signature ​

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

Run a side effect with the wrapped 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 log = (value: unknown) => console.log(value);

// Logs `42` then `84`, and returns `Ok(84)`.
const anOk = result.ok<number, string>(42);
result.inspect(
  log,
  result.map(
    double,
    result.inspect(
      log,
      anOk
    )
  )
);

// Does not log anything, and returns `Err('error')`.
const anErr = result.err<number, string>('error');
result.inspect(
  log,
  result.map(
    double,
    result.inspect(
      log,
      anErr
    )
  )
);

Type Parameters ​

T ​

T

The type of the wrapped value

E ​

E

The type of the error value

Parameters ​

fn ​

(value) => void

The function to call with the wrapped value (only called for Ok)

result ​

Result<T, E>

The Result to inspect

Returns ​

Result<T, E>

The original Result, unchanged

Call Signature ​

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

Run a side effect with the wrapped 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 log = (value: unknown) => console.log(value);

// Logs `42` then `84`, and returns `Ok(84)`.
const anOk = result.ok<number, string>(42);
result.inspect(
  log,
  result.map(
    double,
    result.inspect(
      log,
      anOk
    )
  )
);

// Does not log anything, and returns `Err('error')`.
const anErr = result.err<number, string>('error');
result.inspect(
  log,
  result.map(
    double,
    result.inspect(
      log,
      anErr
    )
  )
);

Type Parameters ​

T ​

T

The type of the wrapped value

E ​

E

The type of the error value

Parameters ​

fn ​

(value) => void

The function to call with the wrapped value (only called for Ok)

Returns ​

The original Result, unchanged

(result) => Result<T, E>