import Service from '@ember/service';

export default class GoogleChartsService extends Service {
  load() {
    return loadJsApi()
      .then(api => loadCoreChart(api))
      .then(() => {
        window.googleChartsLoaded = true;
        document.dispatchEvent(createEvent('googleChartsLoaded'));
      });
  }
}

async function loadScript(src) {
  await new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.src = src;
    script.onload = resolve;
    script.onerror = reject;
    document.body.appendChild(script);
  });
}

async function loadJsApi() {
  if (!window.google) {
    await loadScript('https://www.google.com/jsapi');
  }
  return window.google;
}

async function loadCoreChart(api) {
  await new Promise(resolve => {
    api.load('visualization', '1.0', {
      packages: ['corechart'],
      callback: resolve,
    });
  });
}

function createEvent(name) {
  let event = document.createEvent('Event');
  event.initEvent(name, true, true);
  return event;
}

@@ -93,16 +93,22 @@
 ice %7B%0A  
+async 
 load() %7B
@@ -112,22 +112,31 @@
 ) %7B%0A    
-return
+let api = await
  loadJsA
@@ -143,27 +143,19 @@
 pi()
+;
 %0A    
-  .then(api =%3E
+await
  loa
@@ -173,34 +173,10 @@
 api)
-)%0A      .then(() =%3E %7B%0A    
+;%0A
     
@@ -209,20 +209,16 @@
 = true;%0A
-    
     docu
@@ -276,18 +276,8 @@
 ));%0A
-      %7D);%0A
   %7D%0A

Convert `load()` to async method