-
-
Save addyosmani/71bd44af0022b56723281e76e9faf0aa to your computer and use it in GitHub Desktop.
Simple promise-based script-loader
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
const loader = new loadScript(); | |
loader.load([ | |
'//apis.google.com/js/client:platform.js?onload=startApp', | |
'//cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.10.3/dropbox.min.js' | |
]).then(({length}) => { | |
console.log(`${length} scripts loaded!`); | |
}); |
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
window.loadScript = function () { | |
/** | |
* | |
* @param {string} url | |
* @param {object=} attr | |
* @returns {Promise} | |
*/ | |
const loader = (url, attr) => new Promise((resolve, reject) => { | |
const script = window.document.createElement('script'); | |
script.src = url; | |
script.async = true; | |
script.crossOrigin = 'anonymous'; | |
attr = attr || {}; | |
for (const attrName in attr) { | |
script[attrName] = attr[attrName]; | |
} | |
script.addEventListener('load', () => { | |
resolve(script); | |
}, false); | |
script.addEventListener('error', () => { | |
reject(script); | |
}, false); | |
window.document.body.appendChild(script); | |
}); | |
/** | |
* Loads scripts asynchronously | |
* @param {string|string[]} urls | |
* @param {object=} attr Other script tag attributes | |
* @returns {Promise} | |
*/ | |
this.load = (urls, attr) => { | |
if (!Array.isArray(urls)) { | |
urls = [urls]; | |
} | |
return Promise.all(urls.map(url => loader(url, attr))); | |
} | |
/** | |
* Loads scripts asynchronously. It supports multiple url arguments, so each one will be loaded right after the | |
* previous is loaded. This is a way of chaining dependency loading. | |
* | |
* @param {string|string[]} urls, ... | |
* @returns {Promise} | |
*/ | |
this.loadChain = function (urls) { | |
const args = Array.isArray(arguments) ? arguments : Array.prototype.slice.call(arguments); | |
const p = this.require(args.shift()); | |
const self = this; | |
return args.length ? p.then(() => { | |
self.requireChain(...args); | |
}) : p; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment