链可观察

编程入门 行业动态 更新时间:2024-10-16 18:32:04
本文介绍了链可观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对可观察的链接有疑问.

I have a problem with observable chaining.

我的目标是获得以下结果: {分页,Process []}和Process = {.....,变量[]}

My goal is to have this result : {Pagination, Process[]} and Process = { ....., Variables[]}

我正在调用返回对象 {Pagination,Process []} 的服务,但是 Process 中的变量为空.

I'm calling a service which return an object {Pagination, Process[]} but Variables in Process are empty.

因此,我需要使用变量"属性来完成过程对象.

So i need to complete the process object with Variables properties.

我愿意:

this.processesService.getData().subscribe(() => { // data }); getData: Observable<any> { let processesResult = fromPromise( .... ); return processesResult.pipe( map( (result) => { <= result is list of process without variables let entries = new Array(); result.entries.map( **process** => { result.entries = entries; this.getCompleteProcess(**process.id**).subscribe((_process) => { entries.push(_process) <-- the process with Variables[] for enrich result map }) return result.entries; }) }) ); }

您能帮我解决我的问题吗?我需要等待 this.getCompleteProcess 填充父对象并返回完整的结果

Can you help me to resolve my problem ? i need to wait this.getCompleteProcess to populate parent object and return a complete result

谢谢

推荐答案

我认为rxjs中可以尝试一些运算符(例如forkJoin,concatMap).否则,您还可以使用Observable.create.例如,

I think there are a few operators in rxjs that you could try out (e.g. forkJoin, concatMap). Otherwise what you could also do is to use the Observable.create. For example,

getData: Observable<any> { return Observable.create( (obs) => { let processesResult = fromPromise( .... ).subscribe( (result) => { <= result is list of process let entries = new Array(); let obsArr = []; result.entries.forEach( **process** => { result.entries = entries; obsArr.push(this.getCompleteProcess(**process.id**).pipe(map((_process) => { entries.push(_process) <-- the process is complete and i need it to enrich result map }); forkJoin(...obsArr).subscribe( ([]) => { obs.next(result.entries) => EMIT RESPONSE HERE } ); }) }) ); }

请注意-我尚未对此进行编译或测试.但基本上,这个想法是创建一个可包装的可观察对象,一旦所有逻辑完成,它就会发出一个值.希望这会有所帮助.

Please note - I have not compiled or tested this. But basically, the idea is to create a wrapping observable which emits a value once all the logic is done. Hope this helps.

更多推荐

链可观察

本文发布于:2023-11-13 02:21:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!