UNPKG

storm-load

Version:

Lightweight promise-based script loader

62 lines (54 loc) 1.55 kB
/** * @name storm-load: Lightweight promise-based script loader * @version 1.0.3: Fri, 29 Jun 2018 12:47:50 GMT * @author stormid * @license MIT */ (function(root, factory) { var mod = { exports: {} }; if (typeof exports !== 'undefined'){ mod.exports = exports factory(mod.exports) module.exports = mod.exports.default } else { factory(mod.exports); root.StormLoad = mod.exports.default } }(this, function(exports) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var create = function create(url) { var async = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; return new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.async = async; s.onload = s.onreadystatechange = function () { if (!this.readyState || this.readyState === 'complete') resolve(); }; s.onerror = s.onabort = reject; document.head.appendChild(s); }); }; var synchronous = exports.synchronous = function synchronous(urls) { return new Promise(function (resolve, reject) { var next = function next() { if (!urls.length) return resolve(); create(urls.shift(), false).then(next).catch(reject); }; next(); }); }; exports.default = function (urls) { var async = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; urls = [].concat(urls); if (!async) return synchronous(urls); return Promise.all(urls.map(function (url) { return create(url); })); };; }));