Skip to content

Instantly share code, notes, and snippets.

@neovov
Created February 9, 2017 15:24
Show Gist options
  • Save neovov/53b22af4aab36e303006f6609153ca9f to your computer and use it in GitHub Desktop.
Save neovov/53b22af4aab36e303006f6609153ca9f to your computer and use it in GitHub Desktop.
ReadableStream
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