| 2 | * Simple script loader that returns a promise. |
| 3 | */ |
| 4 | export default function loadScript(url) { |
| 5 | return new Promise((resolve, reject) => { |
| 6 | let done = false; |
| 7 | const head = document.getElementsByTagName('head')[0]; |
| 8 | const script = document.createElement('script'); |
| 9 | script.src = url; |
| 10 | script.onload = script.onreadystatechange = function () { |
| 11 | if ( |
| 12 | !done && |
| 13 | (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') |
| 14 | ) { |
| 15 | done = true; |
| 16 | resolve(); |
| 17 | } else { |
| 18 | reject(); |
| 19 | } |
| 20 | }; |
| 21 | script.onerror = error => reject(error); |
| 22 | head.appendChild(script); |
| 23 | }); |
| 24 | } |