Last active
March 13, 2018 13:45
-
-
Save anthumchris/73ff184a93bf8af52283cd3f7eb91f21 to your computer and use it in GitHub Desktop.
Custom Fetch Response Stream Reader
This file contains 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
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