【发布时间】:2020-11-05 19:28:26
【问题描述】:
@Input() list: string[];
ngOnInit() : void {
this.valueMap = new Map<any, any>();
this.getDataFromService();
this.buildContainer();
}
private getDataFromService(): void {
this.list.forEach(value-> {
this.fetchService(value).subscribe(data ->{
this.valueMap.set(value,data);
}
)
})
}
private buildContainer(): void {
console.log(this.valueMap.size); // shows always 0 even when service returns the data
}
现在我必须在方法 buidlContainer() 中使用这个 valueMap ,
因此我需要先从服务中获取完整的地图。当我在 buildConatainer() 中使用该地图时,它显示我未定义。
我知道这是一些异步调用问题。此外,我不能为 .subscribe() 中的每个值调用方法 buildContainer(),因为这不是更好的主意。 所以,我必须先计算地图,然后再进一步处理..
感谢任何帮助,我无法将服务从返回 Observable 修改为返回 Promise
我想做如下操作
private buildContainer(): void {
for([key,data] of this.valueMap) {
-----some code----
}
}
【问题讨论】:
标签: angular promise async-await rxjs observable