Created
February 9, 2017 15:24
-
-
Save neovov/53b22af4aab36e303006f6609153ca9f to your computer and use it in GitHub Desktop.
ReadableStream
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
new ReadableStream({ | |
start(controller) { | |
return fetch('http://…').then(response => { | |
// In browsers supporting Web Streams, Fetch's response's body is a ReadableStream | |
// Otherwise, in order to stream the response you will have to request some bytes ranges manually | |
// Get a reader on the incoming data | |
const reader = response.body.getReader(); | |
// Read a chunk | |
reader.read().then(function process(result) { | |
console.log('Reading', result.value.length, 'bytes'); | |
return reader.read().then(process); // Continue reading | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment