Skip to content

True Myth / task / inspect

Function: inspect()

Call Signature

inspect<T, E>(fn): (task) => Task<T, E>

Auto-curried, standalone function form of Task.prototype.inspect.

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 Task resolves, and the original Task 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 task from 'true-myth/task';

const double = (n: number) => n * 2;
const log = (value: unknown) => console.log(value);

const logNumber = task.inspect<number, unknown>(log);
const doubleTask = task.map(double);

// Logs `42` then `84`, and returns `Ok(84)`.
let aResolvingTask = task.resolve<number, string>(42);
let aResolvingTask = task.resolve<number, string>(42);
await logNumber(doubleTask(logNumber(aResolvingTask)));

// Does not log anything, and returns `Err('error')`.
let aRejectingTask = task.reject<number, string>('error');
await logNumber(doubleTask(logNumber(aRejectingTask)));

Type Parameters

T

T

The type of the value when the Task resolves successfully.

E

E

The type of the rejection reason when the Task rejects.

Parameters

fn

(value) => void

The function to call with the resolved value (only called on resolution)

Returns

The original Task, unchanged

(task): Task<T, E>

Parameters

task

Task<T, E>

Returns

Task<T, E>

Call Signature

inspect<T, E>(fn, task): Task<T, E>

Standalone function form of Task.prototype.inspect.

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 Task resolves, and the original Task is returned unchanged for further chaining.

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

const double = (n: number) => n * 2;
const log = (value: unknown) => console.log(value);

// Logs `42` then `84`, and returns `Resolved(84)`.
let aResolvingTask = task.resolve<number, string>(42);
task.inspect(
  log,
  task.map(
    double,
    task.inspect(
      log,
      aResolvingTask
    )
  )
);

// Does not log anything, and returns `Err('error')`.
let aRejectingTask = task.reject<number, string>('error');
await task.inspect(
  log,
  task.map(
    double,
    task.inspect(
      log,
      aRejectingTask
    )
  )
);

Type Parameters

T

T

The type of the value when the Task resolves successfully.

E

E

The type of the rejection reason when the Task rejects.

Parameters

fn

(value) => void

The function to call with the resolved value (only called on resolution)

task

Task<T, E>

The Task to inspect

Returns

Task<T, E>

The original Task, unchanged