Skip to content

Instantly share code, notes, and snippets.

@11joselu
Last active February 3, 2020 17:37
Show Gist options
  • Save 11joselu/ce978d31780d36b8beddba285fd61f49 to your computer and use it in GitHub Desktop.
Save 11joselu/ce978d31780d36b8beddba285fd61f49 to your computer and use it in GitHub Desktop.
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