Last active
December 14, 2016 18:42
-
-
Save mrosata/d0476c48ea6d3bf7702e9f02108dc047 to your computer and use it in GitHub Desktop.
FAM - Chrome Dev Summit 2016 snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, ...] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| // ..... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const transform = new TransformStream({ | |
| start(controller) { | |
| // ... | |
| }, | |
| transform(chunk, controller) { | |
| // ... | |
| }, | |
| flush(controller) { | |
| // ... | |
| } | |
| }); | |
| const {readable, writable} = transform; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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