Function forEach

  • Iterates over iterator (iterable/array), calling fn for each value. If fn returns false, iterator cancels.

    Over the default JS forEach function, this one allows you to exit the iteration early.

    Type Parameters

    • T

      Type of iterable's values

    Parameters

    • iterator: Iterable<T> | T[]

      Iterable or array

    • fn: ((v: T) => boolean | void)

      Function to call for each item. If function returns false, iteration cancels

        • (v): boolean | void
        • Parameters

          Returns boolean | void

    Returns void

    import { Sync } from "https://unpkg.com/ixfx/dist/iterables.js"
    Sync.forEach(count(5), () => console.log(`Hi`)); // Prints `Hi` 5x
    Sync.forEach(count(5), i => console.log(i)); // Prints 0 1 2 3 4
    Sync.forEach([0,1,2,3,4], i => console.log(i)); // Prints 0 1 2 3 4

    Use forEach if you want to use an async iterator and async fn.

    Alternatives: