Javascript Runtime Load External Javascript

Aug 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    })})

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.