- From: Jake Archibald <notifications@github.com>
- Date: Sat, 28 Mar 2015 04:11:52 -0700
- To: whatwg/fetch <fetch@noreply.github.com>
Received on Saturday, 28 March 2015 11:12:14 UTC
Can you show (with code) how streams are cumbersome with fetch, in comarrison to an XHR-based API?
With fetch:
```js
fetch(url).then(response => {
var reader = response.body.getReader();
var decoder = new TextDecoder();
function drain(valueSoFar) {
return reader.read().then(function(result) {
valueSoFar += decoder.decode(result.value || new Uint8Array, { stream: !result.done });
if (result.done) return valueSoFar;
return drain(valueSoFar);
});
}
return drain();
}).then(function(fullText) {
console.log(fullText);
});
```
With async/await:
```js
fetch(url).then(async response => {
var reader = response.body.getReader();
var decoder = new TextDecoder();
var fullText = '';
var result;
do {
result = await reader.read();
fullText += decoder.decode(result.value || new Uint8Array, { stream: !result.done });
} while (!result.done);
console.log(fullText);
});
```
---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/28#issuecomment-87209944
Received on Saturday, 28 March 2015 11:12:14 UTC