Reputation: 333
I am new to Javascript and I am running onto the following problem.
I have an array of objects, and I want one of the values of this object to be a resolved promise.
This is my array:
const businesslocationsaux = [
...,
{
id: 1,
type: 'Furniture',
name: 'Colom Forever',
address: 'Placa Pietat 5 Vic',
image: 'https://images.unsplash.com/photo-1472851294608',
option_visited: false,
option_promotion: false,
latlng:{},
distance: 0,
},
...
]
Later on, I am using map to update all the objects in the array and have the latlng value be a resolved promise:
const businesslocations = businesslocationsaux.map( async obj => {
const locationcoordinates = (await Location.geocodeAsync(obj.address))[0]
console.log(locationcoordinates)
return{
...obj,
latlng: locationcoordinates,
}})
console.log(businesslocations)
When I console.log the location coordinates inside the asynchrounous function, I get an object, but when I try to retrieve my 'businesslocations' variable, it returns a promise.
What is the right way to do it?
Thank you soooo much for you help I am stuck :)
Upvotes: 0
Views: 1584
Reputation: 1506
Your gonna have to use Promise.all and wait for it.
const businesslocations = await Promise.all(businesslocationsaux.map( async obj => {
const locationcoordinates = (await Location.geocodeAsync(obj.address))[0]
console.log(locationcoordinates)
return{
...obj,
latlng: locationcoordinates,
}}))
console.log(businesslocations)
Upvotes: 2