Your Google Maps code structure looks something like this:
<script>
function initMap() {
}
var locations =
[
{lat: -31.563910, lng: 147.154312},
{lat: -33.718234, lng: 150.363181}
]
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=API-key&callback=initMap" >
</script>
The thing is, you want to populate locations
via a call to the server (using XMLHttpRequest , i.e. a vanilla JavaScript AJAX call because you don't want to use jQuery).
And you don't want the second script block (the call to the Google Maps API) to execute until locations
has successfully been populated.
How would you achieve this via any of the following:
- A Callback ?
- JavaScript Promises ?
- Any other method you can think of?
Also, is a semaphore, as suggested here a viable way to do this?