yta
    Preparing search index...

    Function fromEvent

    • Add an iterator by attaching an event listener. Will iterate over every event that fire.

      Call .return() on the returned iterable to remove the listener and return from any running iterators.

      import { pipe } from "yta";
      import { filter, forEach, fromEvent, map } from "yta/async";

      const inputEvents = fromEvent("input", numberField);

      stopButton.addEventListener("click", () => {
      // Stops the stream and removes the listener.
      inputEvents.return();
      });

      pipe(
      inputEvents,
      map((event) => event.target.valueAsNumber),
      filter((n) => !Number.isNaN(n)),
      forEach((n) => console.log(n)),
      );
      // Logs every number after an `input` event.

      Note: Because of limitations with async iterators, each iteration happens in the next tick. This might not be ideal if event needs to be handle in the same loop. Look into Observable for a better event driven structure.

      Type Parameters

      • Target extends EventTarget
      • Ev extends Event
      • Key extends string

      Parameters

      Returns AsyncGenerator<Ev, void, void>