export class AsyncScriptLoader { scriptUrl : string ; constructor ( scriptUrl ) { this . scriptUrl = scriptUrl ; } createScriptNode ( ) { const script = document . createElement ( 'script' ) ; script . src = this . scriptUrl ; script . async = true ; return script ; } load ( ) { if ( this . getScriptNode ( ) ) { return Promise . resolve ( ) ; } const script = this . createScriptNode ( ) ; if ( document . body === null ) { return new Promise ( ( resolve ) => { resolve ( ) ; } ) ; } document . body . appendChild ( script ) ; return new Promise ( ( resolve , reject ) => { script . onload = function ( ) { resolve ( ) ; } ; script . onerror = function ( ) { reject ( ) ; } ; } ) ; } getScriptNode ( ) { return document . querySelector ( `script[src="${ this . scriptUrl }"]` ) ; } }