Last active
June 8, 2020 21:40
-
-
Save felquis/0473dfdb355e362ceb5a67097a0934f5 to your computer and use it in GitHub Desktop.
Yet Another Snippet to Load JavaScript files async with promises.
This file contains hidden or 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
/* | |
Usage: loadJavaScriptFiles([{ | |
href: | |
'https://path-to', | |
}, | |
{ | |
href: | |
'https://unpkg.com/cdn-path-to', | |
integrity: | |
'sha384', | |
}, | |
{ | |
href: 'https://unpkg.com/path-to', | |
}, | |
]) | |
*/ | |
const loadJavaScriptFiles = list => { | |
return Promise.all( | |
list.map(file => { | |
return new Promise((resolve, reject) => { | |
const script = document.createElement('script'); | |
Object.keys(file).forEach(propertyName => { | |
script[propertyName] = file[propertyName]; | |
}); | |
script.onload = () => resolve(); | |
script.onerror = () => reject(); | |
document.body.appendChild(script); | |
}); | |
}) | |
); | |
}; | |
module exports loadJavaScriptFiles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment