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

    Function when

    • Creates a task routing each item to one of two tasks, according to a condition.

      Each item is tested once and handed to a single task: the first one if it meets the condition, the second one otherwise. The items both tasks report are interleaved into a single feed, each one as soon as it is ready.

      Warning

      • Incremental: items are emitted as the tasks report them, until the source and both tasks run dry.
      • Streaming: one item is on offer at a time and none is retained, whatever the tasks retain.
      • Stateless: each item is routed on its own, whatever state the tasks carry.
      Warning

      Output order is not preserved: the items of the two tasks interleave and overtake each other according to how long each task takes, though the items of each task keep their own order among themselves.

      Caution

      The two tasks draw in lockstep, so a task idling while still running holds back the other one: pacing and long-running work belong downstream of the routing. A task closing early or running dry stops holding back the other one, while a task that keeps running without ever drawing its feed stalls the routing for good.

      Note

      Each task is invoked once and handed only the items routed to it, so state it initialises on invocation decides on that share alone: a quota or a deduplication covers the items meeting the condition, or the others.

      Note

      A failure of the condition or of either task is reported by the routed feed when it is next advanced.

      Note

      Both tasks and the source are closed when the feed is exhausted, fails or is closed early. Closing waits for the work in flight to settle, so a source idling between items delays it; errors raised while closing are suppressed.

      Type Parameters

      • V

        The type of items drawn from the feed

      • R

        The type of items reported by the tasks

      Parameters

      • test: (value: NoInfer<V>) => boolean

        The condition each item is tested against

      • pass: Task<NoInfer<V>, R>

        The task handed the items meeting test

      • fail: Task<NoInfer<V>, R>

        The task handed the items failing test

      Returns Task<V, R>

      A task yielding the items both tasks report, as they become available

      await pipe(
      (items([1, 2, 3, 4]))
      (when(n => n%2 === 0, map(n => `${n} even`), map(n => `${n} odd`)))
      (toArray())
      ); // "1 odd", "2 even", "3 odd", "4 even", interleaved in no defined order