链可观察 [英] Chain Observable
问题描述
我对可观察的链接有疑问.
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.
这篇关于链可观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!