Re: [whatwg/streams] "await writer.write(bytes);" in example seems wrong (#874)

Sorry, I really should have responded to this sooner. Because the `write()` always uses all of `desiredSize`, desiredSize will go to 0 and stay at 0 until the write finishes. So `writer.ready` cannot fulfill before `writer.write()`.

Additionally, if we don't wait on `writer.write()`, there will be an uncaught rejection, which is not ideal. My preferred way to write the loop is:

```javascript
  await writer.ready; // ensure desiredSize > 0
  while (true) {
    const bytes = new Uint8Array(writer.desiredSize);
    window.crypto.getRandomValues(bytes);

    await writer.write(bytes);
  }
```

Except now it's not a good example of `writer.ready`. Okay, how about

```javascript
  while (true) {
    await writer.ready;
    const bytes = new Uint8Array(1024);
    window.crypto.getRandomValues(bytes);

    writer.write(bytes).catch(() => {});
  }
```

?  Oh, but we're also trying to demonstrate `desiredSize`. Never mind, let's just use `write().catch()` instead of `await write()`.

By the way, why is it `window.crypto.getRandomValues()`? If it was just `crypto.getRandomValues()` it would work in a worker without modification.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/874#issuecomment-364120545

Received on Thursday, 8 February 2018 14:04:24 UTC