I want to extend an response in a loopback4 controller method (TypeScript). I already have an array of objects loaded from database, but each of these objects has to be extended by additional asynchronously loaded data. How do I have to build the forEach/ map method call?
The array looks like this (simplified): [{'ID':1},{'ID':2}]
For each of these objects i want to call a method async getData(record: Record): Promise<Property[]>
As far as i understand, i need to make something like this:
async fetchData()
{
const records = [{'ID':1},{'ID':2}];
// code...
records.forEach((record, index) => {
records[index].Properties = getData(record);
});
// here i need all promises resolved
return records;
}
The result is, that i have an array with pending Promises. What do i have to do, to get all of them resolved before returning the array? I found some code snippets using .map() and/or Promise.all(), but did not found my solution in these examples. Maybe because of lack of knowledge but at the moment i am stuck.