I'm trying to create a matrix of some pairs of data. fetchDataPair() is an async function.
async function fetchData(info_array){
let matrix = []
for(const i of info_array){
let row = []
for (const j of info_array){
if (i != j){
let pair = fetchDataPair(i,j)
row.push(pair)
}else{
row.push(null)
}
}
Promise.all(row).then((resolvedRow)=>{ //resolve all the promises in the row, then push to matrix
matrix.push(resolvedRow)
})
}
//matrix should be an array of arrays of (resolved) promises, but it just returns an empty array
console.log(matrix) //logs []
return matrix
}
This code unfortunately seems to reach return matrix before the rows are pushed into the array, but I can't understand why.
Thanks
EDIT: Fixed the name of the inside function from fetchData to fetchDataPair