【发布时间】:2023-01-14 09:17:41
【问题描述】:
在 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() 完成。
我尝试添加等待
handleEvent($event) {
const value = this.value;
await this.buildData();
//Do some stuff here after buildData is finished.
}
但它不起作用,因为它不返回承诺。
【问题讨论】:
-
rxjs通常需要转向反应式代码而不是命令式代码。因此,一个可行但可能不好的答案是不订阅,返回可观察对象,使用自来水管道运算符调用 handleResponse 作为副作用,并在 handleEvent 中执行this.buildData().subscribe((data)=> {do some stuff})。需要handleResponse的详细信息以及做一些事情虽然是适当的建议 -
请注意,与 Promises 不同,Observable 可以随时间发出多个值
标签: javascript angular rxjs angular2-observables rxjs-observables