rocket-connect / for-emit-of

Turn Node.js Events into Async Iterables.
https://www.npmjs.com/package/for-emit-of
MIT License
9 stars 2 forks source link

yield many #6

Open danstarns opened 4 years ago

danstarns commented 4 years ago

If specified return an array of events. Not to return an array if not specified. yieldMany represents the array capacity, not length.

Feel free to explore other names, yieldMany I'm not sure about

Example definition

interface Options {
  /**
   * Number of items to be yielded. If not specified 1 event (non array) is returned, else an array WHERE `cap` === `yieldMany`.
   */
  yieldMany?: number;
  ...
}

In the wild example

const forEmitOf = require("../dist");
const { EventEmitter } = require("events");

async function main() {
  const emitter = new EventEmitter();

  const iterator = forEmitOf(emitter, {
    yieldMany: 2,
  });

  setInterval(() => {
    emitter.emit("data", {
      id: uuid(),
    });
  }, 100);

  for await (const [a, b] of iterator) {
    await Promise.all([somePromise(a), somePromise(b)]);
  }
}

main();