Function any

  • 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.

    When any item resolves:

    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:

    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.race`: 10ms,20ms,30ms)

    (Note that the order in the resulting AggregateRejection is not guaranteed to be stable!)

    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.

  • 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.

    When any item resolves:

    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:

    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.race`: 10ms,20ms,30ms)

    (Note that the order in the resulting AggregateRejection is not guaranteed to be stable!)

    Type Parameters

    • 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<
        TaskTypesFor<A>[0][number],
        AggregateRejection<TaskTypesFor<A>[1][number][]>,
    >

    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.