Last active
October 9, 2018 08:15
-
-
Save JoostKiens/d82a8680a0f8d6ac6ff560e8b566f9a7 to your computer and use it in GitHub Desktop.
Preload entire video using fetch, returns promise
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
// Preloads entire video, returns promise | |
// NOTE: if a video doesn't exist the fetch will resolve, | |
// but with a Blob type of `plain/text` | |
function preloadVideo(src) { | |
return new Promise((resolve, reject) => { | |
fetch(src) | |
.then(response => response.blob()) | |
.then(blob => | |
/^video\/\w*/.test(blob.type) | |
? resolve(URL.createObjectURL(blob)) | |
: reject() | |
) | |
.catch(reject) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment