Function last

  • Produces a one-value iterable, with the last emitted value.

    import {pipe, last} from 'iter-ops';

    const i = pipe(
    [1, 2, 3],
    last()
    );

    console.log(...i); //=> 3

    console.log(i.first); //=> 3

    When the optional predicate is provided, the last value satisfying it will be emitted.

    import {pipe, last} from 'iter-ops';

    const i = pipe(
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    last(a => a % 2 === 0) // last even number
    );

    console.log(i.first); //=> 8

    Note that the predicate can only return a Promise inside an asynchronous pipeline, or else the Promise will be treated as a truthy value.

    Type Parameters

    • T

    Parameters

    • Optionalcb: ((value: T, index: number, state: IterationState) => boolean | Promise<boolean>)
        • (value, index, state): boolean | Promise<boolean>
        • Parameters

          Returns boolean | Promise<boolean>

    Returns Operation<T, T>