如何等待函数与可观察的解决?



在Angular/Angular2上工作,我得到了类似

的东西
buildData() {
this.services.getData()).pipe(takeUntil(this.ngUnsubscribe))
.subscribe(response => {
this.handleResponse(response);
})

handleEvent($event) {
const value = this.value;
this.buildData();
//Do some stuff here after buildData is finished. 
}

我不知道的是如何等到buildData()完成。

我尝试添加await

handleEvent($event) {
const value = this.value;
await this.buildData();
//Do some stuff here after buildData is finished. 
}

但是它不工作,因为它没有返回一个承诺。

我建议你使用带有tap操作符的管道来触发你的副作用,然后返回可观察对象,这样你可以稍后订阅它或从它触发其他副作用

buildData() {
return this.services.getData()).pipe(takeUntil(this.ngUnsubscribe))
.pipe(
tap((data) => this.handleResponse(response))
);
})

handleEvent($event) {
const value = this.value;
this.buildData()
.pipe(
//your operations
).subscribe();
}

相关内容

最新更新