Skip to content

jQuery `getScript` alternative

js
const getScript = url => new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.src = url;
    script.async = true;
    script.onerror = reject;
    script.onload = script.onreadystatechange = () => {
        if (this.readyState && this.readyState !== 'loaded' && this.readyState !== 'complete') return;
        script.onload = script.onreadystatechange = null;
        resolve();
    };
    document.head.appendChild(script);
});

Sources: