I'm trying to return describedIpList
after all async operations are resolved.
I can see a few ways to solve this without a promise, but I am interested in learning how to do this in best practice with a promise, do I really have to wrap another promise around my function and if so, please show me how to do this correctly.
const ipResolver = (ips) => {
const describedIpList = [];
ips.map((ip) => {
Promise.all([
client.country(ip),
client.city(ip)
])
.then(response => {
[Country, City] = response;
describedIpList.push({
ip,
countryCode: Country.country.isoCode,
postalCode: City.postal.code,
cityName: City.city.names.en,
timeZone: City.location.timeZone,
accuracyRadius: City.location.accuracyRadius,
});
console.log(describedIpList);
})
.catch(err => describedIpList.push({ip, error: err.error}));
return describedIpList;
});
To reiterate. I am looking to return describedIpList
after ips.map()
resolves and then return a response res.json({fullyDescribedIps: ipResolver(ips)});