- From: Domenic Denicola <notifications@github.com>
- Date: Fri, 20 Mar 2015 07:01:17 -0700
- To: whatwg/streams <streams@noreply.github.com>
- Message-ID: <whatwg/streams/pull/302/c84022419@github.com>
@wanderview great question. Fortunately I think we took care of it with the reader design :).
To read from a stream, you need to acquire a reader: so, `var reader = resp.body.getReader(); var readPromise = reader.read()`.
If anyone else wants to read from the stream---say, the clone procedure---they will need to also get a reader. But, they cannot get a reader at the same time as you. So, you need to release your reader first.
AND! You're not allowed to release your reader, until all of the read-promises you've asked the reader to create, have settled.
So, the example is more like:
```js
var reader = resp.body.getReader();
var readPromise = reader.read();
// this will throw, since it needs to get a reader, but the stream is already locked
try {
var resp2 = resp.clone();
} catch (e) { }
// this will also throw, since we haven't waited for readPromise to settle
try {
reader.releaseLock();
} catch (e) { }
// this will work:
readPromise.then(_ => {
reader.releaseLock();
var resp2 = resp.clone();
});
```
Make sense?
---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/pull/302#issuecomment-84022419
Received on Friday, 20 March 2015 14:01:48 UTC