Javascript Runtime Load External Javascript

August 21, 2018
const loadedScripts = {}

loadScript = (src) => {
  if (!(src in loadedScripts)) {
    const promise = new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.onload = resolve
      script.onerror = reject
      script.src = src
      document.getElementsByTagName('head')[0].appendChild(script)
    })
    loadedScripts[src] = promise
  }
  return loadedScripts[src]
}

Load Google Maps

const googleMapPromise = loadScript('https://maps.googleapis.com/maps/api/js?key=123456789&libraries=places'

googleMapPromise.then(() => {
  var mapCenter = new google.maps.LatLng(-33.8617374,151.2021291)

  map = new google.maps.Map(document.getElementById('map'), {
      center: mapCenter,
      zoom: 15
    })
})
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.