I have a method in a service that iterates synchronous with a for loop. In this for loop I have an async call and i want only to continue the loop when the async call is done. Do I need some kind of eventemitter that emits to the async called value? Couse I now for a fact the async call will open up i different thread so the for loop iterates, through without even waiting if the call is done. Here my method from the service:
private mapHeroTypeToDisplayHeroType(heroList: TroopsHeroesAndSpellType[]): Observable<HeroDisplay[]> {
const heroesWithImgArray: HeroDisplay[] = [];
for (const hero of heroList) {
Object.keys(Heroes).filter(HeroesKey => {
if (hero.name === Heroes[HeroesKey]) {
Object.keys(HeroesImg).filter(HeroesImgKey => {
if (<Heroes>HeroesKey as string === <HeroesImg>HeroesImgKey as string) {
this.storage.ref(HeroesImg[HeroesImgKey]).getDownloadURL().subscribe((data: HeroesImg) => {
let heroObj = {
name: hero.name,
level: hero.level,
maxLevel: hero.maxLevel,
village: hero.village,
heroImg: data
};
heroesWithImgArray.push(heroObj);
});
}
});
}
});
}
return Observable.of(heroesWithImgArray);
}