I have an async function which should resolve multiple promises, push some data into an array and then return said array. It looks like this
const responses = await Promise.all(promises)
await this.resolveResponses(responses, data, ...).then(
() => {
return data
}
)
async resolveResponses(responses: Response[], data: any[],...) {
responses.forEach(async (response) => {
await response.json().then((responseData) => {
console.log(responseData);
data.push(//something from responseData)
});
});
}
But it goes to return data
before finishing resolveResponses. From debugging it seems after reaching await response.json() it doesn't go further to the .then() clause, but returns data from calling function. And after that comes again and finishes .then from resolveResponses. What is wrong with it?
Tried also a one liner with the same result, something like
await Promise.all(promises)
.then(promises => {promises.forEach(async promise => {
const response = await promise.json()
if (response.eeResponse == true)
data.push(//something from response)
})}).then(() => { return data })