Skip to content

True Myth / task / any

Function: any()

Call Signature

any(tasks): Task<never, AggregateRejection<[]>>

Given an array of tasks, return a new Task which resolves once any of the tasks resolves successfully, or which rejects once all the tasks have rejected.

Examples

When any item resolves:

ts
import { any, timer } from 'true-myth/task';

let anyTask = any([
  timer(20),
  timer(10),
  timer(30),
]);

let result = await anyTask;
console.log(result.toString()); // Ok(10);

When all items reject:

ts
import Task, { timer } from 'true-myth/task';

let anyTask = any([
  timer(20).andThen((time) => Task.reject(`${time}ms`)),
  timer(10).andThen((time) => Task.reject(`${time}ms`)),
  timer(30).andThen((time) => Task.reject(`${time}ms`)),
]);

let result = await anyTask;
console.log(result.toString()); // Err(AggregateRejection: `Task.any`: 10ms,20ms,30ms)

The order in the resulting AggregateRejection is guaranteed to be stable and to match the order of the tasks passed in.

Parameters

tasks

[]

The set of tasks to check for any resolution.

Returns

Task<never, AggregateRejection<[]>>

A Task which is either Resolved with the value of the first task to resolve, or Rejected with the rejection reasons for all the tasks passed in in an AggregateRejection. Note that the order of the rejection reasons is not guaranteed.

Call Signature

any<A>(tasks): Task<{ -readonly [P in string | number | symbol]: ResolvesTo<A[P<P>]> }[number], AggregateRejection<[...{ -readonly [P in string | number | symbol]: RejectsWith<A[P<P>]> }[]]>>

Given an array of tasks, return a new Task which resolves once any of the tasks resolves successfully, or which rejects once all the tasks have rejected.

Examples

When any item resolves:

ts
import { any, timer } from 'true-myth/task';

let anyTask = any([
  timer(20),
  timer(10),
  timer(30),
]);

let result = await anyTask;
console.log(result.toString()); // Ok(10);

When all items reject:

ts
import Task, { timer } from 'true-myth/task';

let anyTask = any([
  timer(20).andThen((time) => Task.reject(`${time}ms`)),
  timer(10).andThen((time) => Task.reject(`${time}ms`)),
  timer(30).andThen((time) => Task.reject(`${time}ms`)),
]);

let result = await anyTask;
console.log(result.toString()); // Err(AggregateRejection: `Task.any`: 10ms,20ms,30ms)

The order in the resulting AggregateRejection is guaranteed to be stable and to match the order of the tasks passed in.

Type Parameters

A

A extends readonly AnyTask[]

The type of the array or tuple of tasks.

Parameters

tasks

A

The set of tasks to check for any resolution.

Returns

Task<{ -readonly [P in string | number | symbol]: ResolvesTo<A[P<P>]> }[number], AggregateRejection<[...{ -readonly [P in string | number | symbol]: RejectsWith<A[P<P>]> }[]]>>

A Task which is either Resolved with the value of the first task to resolve, or Rejected with the rejection reasons for all the tasks passed in in an AggregateRejection. Note that the order of the rejection reasons is not guaranteed.