Last active
August 7, 2018 11:14
-
-
Save amitasaurus/54d614b5352c715b0f07cae27e2c4c08 to your computer and use it in GitHub Desktop.
loadScript.js promisified
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
function loadScript(src) { | |
return new Promise(function(resolve, reject) { | |
let script = document.createElement('script'); | |
script.src = src; | |
script.onload = () => resolve(script); | |
script.onerror = () => reject(new Error("Script load error: " + src)); | |
document.head.append(script); | |
}); | |
} | |
//Usage | |
/*loadScript("https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.2.0/lodash.js").then((success) => { | |
Script laoded do something | |
}) | |
.catch((err) => { | |
Script failed to load , do something else | |
})*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment