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

Timeout occurring when it shouldn't #11

Closed Farenheith closed 4 years ago

Farenheith commented 4 years ago

I got a situation where the timeout is thrown even though the duration between event emission is lesser than it.

This issue occurs because of the combination of the delay of the "for await of" operation and the timeout itself. I was able to create a test case where this issue happens

    it("should not throw timeout for delays caused by the 'for await of' using the generated Async Iterable", async () => {
      const emitter = new EventEmitter();

      const iterator = forEmitOf<{ message: string }>(emitter, {
        firstEventTimeout: 100,
        inBetweenTimeout: 100,
      });

      setTimeout(async () => {
        emitter.emit("data", { message: "test1" });
        emitter.emit("data", { message: "test2" });
        await sleep(80);
        emitter.emit("data", { message: "test3" });
        await sleep(80);
        emitter.emit("data", { message: "test4" });
        emitter.emit("end");
      }, 10);

      let result = "";

      for await (const chunk of iterator) {
        await sleep(50);
        result += chunk.message;
      }

      expect(result).to.equal("test1test2test3test4");
    });
danstarns commented 4 years ago

@Farenheith Nice 😎