Re: [fetch] Question — Why can I not read the stream again ? (#196)

@tusharmath 

> Intuitively all this time we have been used to thinking that if we have access to the response, we will have access to the body also. And suddenly here I can only access the body once!

This isn't quite true. If you make an XHR request with `responseType` "json" you cannot then get the response as text.

With `response.json()` you're asking the body to be read as JSON. If you want the JSON multiple times you can store it in a variable…

```js
fetch(url).then(r => r.json()).then(data => {
  console.log(data); // this works
  console.log(data); // and this works
});
```

…or store the promise in a variable…

```js
const jsonPromise = fetch(url).then(r => r.json());
jsonPromise.then(data => console.log(data)); // this works
jsonPromise.then(data => console.log(data)); // and this works
```

> I would have preferred if the body was exposed as a hot observable.

As @annevk says, this is the plan. You'll still only be able to read that stream once though (unless you clone the response or tee the stream).

Here's an example using streams to search a response https://jsbin.com/tepohi/edit?js,console

---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/196#issuecomment-171958775

Received on Friday, 15 January 2016 13:21:36 UTC