1

My question concerns error handling of a reader from a ReadableStreamDefaultReader. I'm guessing that a Promise rejection from reader.read() would usually indicate a network failure for a fetch request (in which case i'm guessing that it is NOT safe to continue reading and I should just stop using that reader entirely), but perhaps there are other possible errors in which it IS safe to continue reading?

So in what scenarios can I continue reading after I receive a Promise rejection from reader.read() and in what scenarios should i scrap the reader entirely? Do I need to introspect on the type of error in order to make a decision?

thurt
  • 882
  • 1
  • 10
  • 25
  • What do you mean by "save"? That nothing will blow up? – Bergi Feb 04 '18 at 16:58
  • Of course you should inspect the error if you want to make a decision. – Bergi Feb 04 '18 at 16:58
  • so i have `const chunk = await reader.read()` call inside an permanent loop `while (true)` which only breaks when it encounters a `chunk.done === true`. i don't want the potential for an infinite loop so im thinking to just break on the first `reader.read()` rejection – thurt Feb 04 '18 at 17:05
  • 1
    An `await`ed rejection behaves like an exception - which will break your loop as well. You don't have to do anything special about it. – Bergi Feb 04 '18 at 17:09
  • interesting, thanks! i was not aware of that behavior. so i still have a question: if i catch that exception, can i then continue to `reader.read()` or will that cause another rejection? i dont understand whether a single `reader.read()` rejection invalidate the whole stream – thurt Feb 04 '18 at 17:14

1 Answers1

1

A stream cannot recover from an error. If it is errored, reading from it will always return a rejected promise.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375