Skip to content

Instantly share code, notes, and snippets.

@mrosata
Last active December 14, 2016 18:42
Show Gist options
  • Select an option

  • Save mrosata/d0476c48ea6d3bf7702e9f02108dc047 to your computer and use it in GitHub Desktop.

Select an option

Save mrosata/d0476c48ea6d3bf7702e9f02108dc047 to your computer and use it in GitHub Desktop.
FAM - Chrome Dev Summit 2016 snippet
const response = await fetch(url);
const reader = response.body.getReader();
while (true) {
const {done, value} = await reader.read();
if (done)
break;
// Do something with value otherwise
console.log(value);
}
const response = await fetch(url);
for await ( const value of response.body ) {
console.log(value);
}
// We get bytes back
// [ 72, 101, 108, .... ]
// [ 121, 111, ....]
// [ 113, 12, ...]
const response = await fetch(url);
const decoder = new TextDecoder();
for await ( const value of response.body ) {
console.log(
decoder.decode(value, {stream: true})
);
}
// We now get text
// Hello everyo
// ne How are y
// .....
const transform = new TransformStream({
start(controller) {
// ...
},
transform(chunk, controller) {
// ...
},
flush(controller) {
// ...
}
});
const {readable, writable} = transform;
const response = await fetch(url);
const stream = response.body.pipeThrough(new TextDecoder());
for await ( const value of stream ) {
console.log(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment