Closed ssuess closed 3 years ago
You seem confused using await and then, the code should be like this
const combo = {}
combo.cprefixes = JSON.parse(JSON.stringify(this.prefixes))
combo.cmyn = this.myn
console.log('combo', combo) // looks good here
const getAnswers = greenlet(async combo => {
const pkeys = Object.keys(combo.cprefixes)
const promises = []
console.log(combo) // looks good here too
for (const key of pkeys) {
const url = 'https://my.data.api?myn=' + cmyn + '&prefix=' + combo.cprefixes[key].prefix
const res = await fetch(url).then(response => response.json())
promises.push(res)
}
console.log('promises:', JSON.parse(JSON.stringify(promises))) // looks good here too
var results = await Promise.all(promises);
const keys = Object.keys(results)
console.log('green results: ', results) // looks good here too
for (const key of keys) {
combo.cprefixes.find(x => x.prefix === results[key].data.prefix).name = results[key].data.name
}
console.log('modified prefixes:', combo.cprefixes) // looks good here too
return combo.cprefixes
});
getAnswers(combo)
.then((response) => {
console.log('got a response?:', response) // undefined here, why?
})
I will try that, thank you!
@ssuess you're actually just missing a return statement:
return await Promise.all(....);
I am flummoxed by the following, while I can log exactly the result I am looking for, it comes back as
undefined
in my return