Last active
February 3, 2020 17:37
-
-
Save 11joselu/ce978d31780d36b8beddba285fd61f49 to your computer and use it in GitHub Desktop.
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
function importScript(path, events = []) { | |
return new Promise(function(resolve, reject) { | |
if (Array.isArray(events) && events.length) { | |
// Por cada evento lo añadimos a window tan sólo una vez | |
return events.forEach(function(event) { | |
window.addEventListener( | |
event, | |
function() { | |
loadScript(path) | |
.then(resolve) | |
.catch(reject); | |
}, | |
{ once: true } | |
); | |
}); | |
} | |
return loadScript(path) | |
.then(resolve) | |
.catch(reject); | |
}); | |
} | |
// 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