There is a list of unlimited cars in backend. Each car has an id (unique id) and a name (car model name).
I have the list of car IDs, example:
const carIds = ['abc','xyz'];
and I need to fetch respective car names for the above car Ids. I tried the below code, but its not working at all, what is that I'm missing ?
const carIds = ['abc','xyz']; // unique ids
const carsList = [];
for (const carId of carIds) {
this.api.getCar(carId).pipe(
map(response => {
carsList.push({
name: response.name,
id: carId
});
})
);
}
console.log(carsList); // When I run this, its printing nothing.
Expected Output:
carsList = [{
id: 'abc',
name: 'benz'
},{
id: 'xyz',
name: 'mercede'
}]
Thanks in advance.
Promise.all([ promise1, promise2 ])to run 2 promises at the same time.