Last active
February 3, 2020 17:37
-
-
Save 11joselu/288566147311c9e5439fab90151a0848 to your computer and use it in GitHub Desktop.
Load Script Promises
This file contains hidden or 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
// Creamos un tag Script y lo añadimos al Head | |
// Añadimos los enventos onload/onerror que nos | |
// permitirá resolver/rechazar las promesas | |
// Y eliminamos el elemento del DOM | |
const loadScript = function(path) { | |
const script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.src = path; | |
script.async = true; | |
entry.promise = new Promise((resolve, reject) => { | |
script.onload = resolve; | |
entry.resolve = resolve; | |
script.onerror = reject; | |
}); | |
document.head.appendChild(script); | |
script.remove(); | |
return entry.promise; | |
}; | |
// Se han omitido procesos de caché etc… con el fin de hacer la función más corta y legible |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment