Skip to content

Instantly share code, notes, and snippets.

@anthumchris
Created February 28, 2018 19:40
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/39ebe19f2f226e2858e0511febf2a002 to your computer and use it in GitHub Desktop.
Save anthumchris/39ebe19f2f226e2858e0511febf2a002 to your computer and use it in GitHub Desktop.
/* To test, initiate the fetch() and interrupt your network conneciton
* (e.g. turn off wireless). The originating ReadableStreamDefaultReader.read()
* error is lost and unavailable to application's outermost Promise.reject catch()
*/
fetch('https://fetch-progress.anthum.com/10kbps/images/sunrise-baseline.jpg')
.then(response => {
const reader = response.body.getReader();
return new Response(
new ReadableStream({
start(controller) {
read()
function read() {
reader.read().then(({done, value}) => {
if (done) {
controller.close();
return;
}
controller.enqueue(value);
read();
}).catch(error => {
console.error('error at read()', error);
controller.error(error)
})
}
}
})
)
})
.then(response => response.arrayBuffer())
.catch(error => console.error(error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment