Skip to content

Instantly share code, notes, and snippets.

@Shuumatsu
Created January 12, 2018 08:28
Show Gist options
  • Save Shuumatsu/8543a172de13767b112dd44d2600c9f3 to your computer and use it in GitHub Desktop.
Save Shuumatsu/8543a172de13767b112dd44d2600c9f3 to your computer and use it in GitHub Desktop.
const cache = new WeakMap()
export default src => new Promise((resolve, reject) => {
if (cache.has(src))
resolve(cache.get(src))
const audio = document.createElement('audio')
audio.addEventListener('loadedmetadata', e => {
const duration = audio.duration
cache.set(src, duration)
resolve(duration)
})
audio.addEventListener('error', reject)
audio.src = src
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment