I have the following code where I iterate through a list with names and use each name to fetch in this case the respective house. This works fine, but I want to include all promises to a Promise.all()
- how can I dynamically add a Promise for each name to a Promise.all()
? What's good practice here?
list = [
'Name1',
'Name2',
'Name3'
];
this.houses = new BehaviorSubject<any>([]);
const promises = this.greatHouses.map(name => {
let house;
return this.fetchHouse(name).then((result: any[]) => {
house = result[0];
return result[0];
}).then((result: any) => {
return this.fetchLord(result.currentLord);
}).then((result: any) => {
const currentLord = Characters.default.find(char => char.characterName === result.name);
return [{...house, currentLord}];
});
});
Promise.all(promises).then(values => {
console.log(values);
});
fetchHouse(name: string) {
return this.http.get(`${this.housesUrl}`, {
params: {
name
}
}).toPromise();
}
fetchLord(url: string) {
return this.http.get(url).toPromise();
}