Importing external libraries
/**
* Loads a JavaScript file and returns a Promise for when it is loaded
* https://aaronsmith.online/articles/easily-load-an-external-script-using-javascript/
*/
const loadScript = src => {
return new Promise((resolve, reject) => {
const script = document.createElement('script')
script.type = 'text/javascript'
script.onload = resolve
script.onerror = reject
script.src = src
document.head.append(script)
})
}
loadScript('script1.js')
.then(() => loadScript('script2.js'))
.then(() => loadScript('script3.min.js'))
.then(() => {
// Execute library-dependent code here
// Consider using function(s) to keep the code more legible
})
.catch(() => console.error('Failed to load external libraries.'))Last updated
