Skip to content

Instantly share code, notes, and snippets.

@JoostKiens
Last active October 9, 2018 08:15
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 JoostKiens/d82a8680a0f8d6ac6ff560e8b566f9a7 to your computer and use it in GitHub Desktop.
Save JoostKiens/d82a8680a0f8d6ac6ff560e8b566f9a7 to your computer and use it in GitHub Desktop.
Preload entire video using fetch, returns promise
// 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