I am trying to get some data from an external API, but when I loop am trying to push into an empty array it stills empty. I think it has something to do with accessing the variable inside the callback or the asynchronous behavior.
const ListProducts = asyncHandler(async response => {
var array = [];
response.forEach(p => {
anExternalAPI.showProducts({
id: p.id,
callback: res => {
console.log(res); // showing data
array.push(res);
}
});
});
console.log("products: ", array); // it is empty
return array;
});