Working in NodeJS, I have a function that returns an array of objects. Inside these objects, many fields are populated by awaited promises, returned from an API. However, for some reason, this means that I can't operate within the array at all. For example:
I have a function to fetch from the api,
export async function someFunction(){
let item = `--API URL HERE--`
let res = await fetch(item);
let data = await res.json();
return data;
}
in another async function, I use that data to built an array of objects, some of the fields of that object being populated with information from that api, called with an awaited function like:
let field = await exportedFile.someFunction();
Once I have that array of objects built, I can log it with console.log(array);
, and see the fully populated array. However, console.log(array[1]);
returns undefined, as does console.log(await array[1]);
further, attempting the spread operator to offload it into another array returns an empty array, and attempting to stringify it results in []
. Since console.log(array);
does work and does print the correct data, I know its being built at some point, but some whatever reason it appears the array operators refuse to respect await.
What am I doing wrong, and how can I work with this array?