Last active
July 6, 2019 11:52
-
-
Save adinan-cenci/71474bf1d64156124ac22ed469a8560a to your computer and use it in GitHub Desktop.
Asynchronously load javascript files
This file contains 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
async function loadScript(src) | |
{ | |
return new Promise(async function(success, fail) | |
{ | |
var script; | |
script = document.createElement('script'); | |
script.async = true; | |
script.type = 'text/javascript'; | |
script.onload = function(e) | |
{ | |
success('SCRIPT: loaded successfully'); | |
}; | |
script.onerror = function(e) | |
{ | |
fail('SCRIPT: failed to load'); | |
}; | |
script.src = src; | |
document.body.appendChild(script); | |
}); | |
} | |
/* | |
Example: | |
loadScript('my-script.js').then(value => console.log(value), value => console.log(value)); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment