Skip to content

Instantly share code, notes, and snippets.

@anthumchris
Last active March 13, 2018 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthumchris/73ff184a93bf8af52283cd3f7eb91f21 to your computer and use it in GitHub Desktop.
Save anthumchris/73ff184a93bf8af52283cd3f7eb91f21 to your computer and use it in GitHub Desktop.
Custom Fetch Response Stream Reader
fetch('the-best-song-ever.mp3')
.then(response => {
if (!response.body) {
throw Error("ReadableStream is not yet supported in this browser. See https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream")
return response;
}
const reader = response.body.getReader();
read()
function read() {
reader.read().then(({done, value}) => {
if (done) return;
// TODO - implement custom audio decoding/playing functionality here
console.log(`read ${value.byteLength} bytes`);
read();
});
}
})
.catch(error => console.error(error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment