I'm trying to figure out how to store promise return stages into variables. In this use case, I'm using google maps and google's geocoder to transform a JSON file of addresses into an array of lat lng objects in order to make markers, but I'm curious regardless. It's been done through a single promise, but as I'm forced to loop through the data and execute the call, I can't call another function and leave the promise, as I have to repeat the promise eventually. This means I need to store the object in a separate variable. See the sections of code below.
address.json
{
"address" : "Oosterpark 9",
"city" : "Amsterdam",
"name" : "Onze Lieve Vrouwe Gasthuis"
"zipcode" : "1091 AC"
},
{
"address" : "Jan Tooropstraat 164",
"city" : "Amsterdam",
"name" : "Sint Lucas Andreas Hospital"
"zipcode" : "1061 AE"
}
Map.js
makeMarker(a){
Geocode.setApiKey(this.apiKey);
let arr = [];
for(let i=0; i < a.length; i++){
let address = b[i].address.replace(" ","+") + ',+'+b[i].zipcode.replace(" ","+")+',+'+b[i].city;
arr[i] = Geocode.fromAddress(address).then(
response => {
const {lat, lng} = response.results[0].geometry.location;
console.log(lat, lng);
return {lat: lat, lng: lng};
}
);
}
return arr;
}
In this case the above function is called on in the constructor where a variable is waiting to be defined by the outcome of the makeMarker function. In this case, it only returns [promise, promise], when it's supposed to return [{lat: __, lng: __,}, {lat: __, lng: __,}].
I hope to hear some of your ideas on how to solve this use case!