将嵌套的rxjs conf缩放到aysnc-wait?
我总是遇到嵌套的con缩,后者的HTTP请求基于前一个结果。 请参阅下面的示例,我需要获取FormConfig,然后使用FormConfig ID获取应用程序记录,然后获取XXX。它是否更可读取和可维护的转动嵌套的RXJS辅助映射到Aysnc等待( 避免回调地狱 )?优点和缺点?哪一个更容易处理错误处理?
一般嵌套concatmap
ngOnInit(){
this.http.get(`/app-config/${this.appType}`)
.pipe(
concatMap(formConfig => {
this.formConfig = formConfig;
return this.http.get(`/appl/${this.formConfig.id}`);
}),
concatMap(applRec => {
this.applRec = applRec;
return this.http.get(`/xxx/${this.applRec.recNo}`)
})
tap((val) => {this.xxx = val})
....
).subscribe();
}
转向异步等待topromise
constructor(){
this.init().then();
}
async init() {
this.formConfig = await this.http.get(`/app-config/${this.appType}`).toPromise();
this.applRec = await this.http.get(`/appl/${this.formConfig.id}`).toPromise();
this.xxx = await this.http.get(`/xxx/${this.applRec.recNo}`).toPromise();
}
I always encounter nested concatMap, the latter http requests base on the former results.
See below example, I need get formConfig and then use the formConfig id to get application record, then xxx. Is it more readable and maintainable turning nested rxjs concatMap to aysnc await (Avoid callback hell)? Pros and cons? which one is easier to process error handling?
General nested concatMap
ngOnInit(){
this.http.get(`/app-config/${this.appType}`)
.pipe(
concatMap(formConfig => {
this.formConfig = formConfig;
return this.http.get(`/appl/${this.formConfig.id}`);
}),
concatMap(applRec => {
this.applRec = applRec;
return this.http.get(`/xxx/${this.applRec.recNo}`)
})
tap((val) => {this.xxx = val})
....
).subscribe();
}
Turn to async await toPromise
constructor(){
this.init().then();
}
async init() {
this.formConfig = await this.http.get(`/app-config/${this.appType}`).toPromise();
this.applRec = await this.http.get(`/appl/${this.formConfig.id}`).toPromise();
this.xxx = await this.http.get(`/xxx/${this.applRec.recNo}`).toPromise();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
topRomise()
已弃用并正在删除,因此您可能应该避免使用它。有关此信息的更多信息: https://indepth.dev/posts/1287/rxjs-heads-up-tpromise-is-is-being-deprecated#why-is-is-is-is-thisthis-the-thappening-to-to-to-to-to-to-to-me 。如果要使用
async
/等待
/承诺您可以使用firstValueFrom()
。在此处的文档中概述了使用它的缺点: https://rxjs.dev/api/索引/函数/firstValuefrom因此,承诺的缺点是您需要添加超时或冒着内存泄漏的风险。但是,我认为当使用HTTPCLIENT服务的观察力时,您不必担心这一点,我从未见过一个人没有发出值或完整的值。
由于您只是在寻找单个值而不是可观察到的流,因此在这里使用承诺的任何缺点。您也可以将ngoninit标记为
async
。toPromise()
is deprecated and is being removed, so you should probably avoid it. More info on that here: https://indepth.dev/posts/1287/rxjs-heads-up-topromise-is-being-deprecated#why-is-this-happening-to-me.If you want to use
async
/await
/ promises you can usefirstValueFrom()
. The cons of using it are outlined in the docs here: https://rxjs.dev/api/index/function/firstValueFromSo the disadvantage of promises is that you need to add a timeout or you risk a memory leak. However, I don't think you have to worry about this when using the observables from the HttpClient service, I've never seen one not emit a value or complete.
Since you're only looking for single values and not observable streams, I don't see any downside to using promises here. You can also just label ngOnInit as
async
.