Skip to content

Instantly share code, notes, and snippets.

@adinan-cenci
Last active July 6, 2019 11:52
Show Gist options
  • Save adinan-cenci/71474bf1d64156124ac22ed469a8560a to your computer and use it in GitHub Desktop.
Save adinan-cenci/71474bf1d64156124ac22ed469a8560a to your computer and use it in GitHub Desktop.
Asynchronously load javascript files
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