@metreeca/flow - v0.9.21
    Preparing search index...

    Function flatMap

    • Creates a task expanding each item into a data source.

      Each item is converted into a Data value and replaced in the feed by the items that value contributes, according to its shape; conversion is lazy, one item at a time, and expansions are emitted in source order. An item expanding to nothing simply drops out of the feed.

      The mapper returns a batch of items rather than a single value, so an item replaced by exactly one value expands to an array wrapping it.

      Type Parameters

      • V

        The type of input items

      • R

        The type of output items

      Parameters

      • mapper: (item: V) => Awaitable<Data<R>>

        The function converting each item into the data source replacing it; an empty result, as well as an undefined value within the expansion, contributes nothing, so expansion doubles as filtering

      Returns Task<V, R>

      A task yielding the items contributed by the expansion of each source item

      await pipe(
      (items(1, 2, 3))
      (flatMap(n => [n, n*2]))
      (toArray())
      ); // [1, 2, 2, 4, 3, 6]

      map to replace each item with a single value