Created
February 28, 2018 19:40
-
-
Save anthumchris/39ebe19f2f226e2858e0511febf2a002 to your computer and use it in GitHub Desktop.
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
/* 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