Iterates over an async iterable or array, calling fn for each value, with optional interval between each loop. If the async fn returns false, iterator cancels.

import { forEach } from "https://unpkg.com/ixfx/dist/flow.js"
// Prints items from array every second
await forEach([0,1,2,3], i => console.log(i), 1000);
// Retry up to five times, with 5 seconds between each attempt
await forEach(count(5), i=> {
try {
await doSomething();
return false; // Succeeded, exit early
} catch (ex) {
console.log(ex);
return true; // Keep trying
}
}, 5000);
  • Type Parameters

    • T

    Parameters

    • iterator: AsyncIterable<T> | T[]

      Iterable thing to loop over

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

      Function to invoke on each item. If it returns false loop ends.

        • (v?):
              | boolean
              | void
              | Promise<void>
              | Promise<boolean>
        • Parameters

          • Optionalv: T

          Returns
              | boolean
              | void
              | Promise<void>
              | Promise<boolean>

    • options: Partial<ForEachOptions> = {}

      Options

    Returns Promise<void>