Skip to content

Instantly share code, notes, and snippets.

@felquis
Last active June 8, 2020 21:40
Show Gist options
  • Save felquis/0473dfdb355e362ceb5a67097a0934f5 to your computer and use it in GitHub Desktop.
Save felquis/0473dfdb355e362ceb5a67097a0934f5 to your computer and use it in GitHub Desktop.
Yet Another Snippet to Load JavaScript files async with promises.
/*
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