Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled promise rejection with TypeError when using async iterator #68

Closed
acostalima opened this issue Nov 12, 2020 · 3 comments
Closed

Comments

@acostalima
Copy link

acostalima commented Nov 12, 2020

Both repros below error out the stream after some time.

In test 1, reader.read() read throws as expected.

In test 2, the output is as follows:

Possible Unhandled Promise Rejection (id: 0):
TypeError: reader._closedPromise_reject is not a function. (In 'reader._closedPromise_reject(reason)', 'reader._closedPromise_reject' is undefined)

If I wrap controller.error() with a try/catch block, the error above is caught and the try/catch block of for..await behaves exactly the same as in test 1.

Is this the intended behavior or am I doing something wrong? 🤔

NOTE: I'm using the polyfill in React Native.

Test case 1

const readableStreamTest1 = async () => {
  const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  let controller;

  const rs = new ReadableStream({
    async pull(c) {
      controller = c;
      await delay(250);
      c.enqueue('readable');
      await delay(250);
      c.enqueue('stream');
      await delay(250);
      c.enqueue('polyfill');
      c.close();
    },
  });

  const reader = rs.getReader();

  const read = () => {
    return reader
      .read()
      .then(({done, value}) => {
        if (done) {
          console.log('readableStreamTest1 done');
          return;
        }

        console.log('readableStreamTest1 read', {value});

        return read();
      })
      .catch((error) => console.error('readableStreamTest1 read', {error}));
  };

  read();
  await delay(500);
  controller.error(new Error('error'));
};

Test case 2

const readableStreamTest2 = async () => {
  const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  let controller;

  const rs = new ReadableStream({
    async pull(c) {
      controller = c;
      await delay(250);
      c.enqueue('readable');
      await delay(250);
      c.enqueue('stream');
      await delay(250);
      c.enqueue('polyfill');
      c.close();
    },
  });

  const read = async () => {
    try {
      for await (const chunk of rs) {
        console.log('readableStreamTest2 read', {chunk});
      }
    } catch (error) {
      console.error('readableStreamTest2 read', {error});
    }
  };

  read();
  await delay(500);
  try {
    controller.error(new Error('error'));
  } catch (error) {
    console.error('readableStreamTest2 controller.error', {error});
  }
};
@MattiasBuelens
Copy link
Owner

Thanks for reporting!

A similar issue was reported yesterday (#66), which is fixed in the latest 3.0.1 release. Could you try updating to that version?

@acostalima
Copy link
Author

acostalima commented Nov 12, 2020

@MattiasBuelens will do. Sorry for missing that earlier report! 🙏

Thanks!

@acostalima
Copy link
Author

@MattiasBuelens I confirm it's fixed now. 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants