我可以订阅内部 运行 forkJoin 的方法吗
Can I subscribe to method which is running forkJoin inside
我有一个非常基本的问题。
我确实有一个包装 forkJoin
call
的方法
ngOnInit(): void {
getCustomData();
forkJoin([
this.serviceTwo.test(),
this.serviceThree.test()
])
.pipe(doSomeStuff())
.subscribe([test1, test2=> {
//someActions
})
}
getCustomData(): void {
forkJoin([
this.service.doOne(),
this.service.doTwo()
])
.pipe(doSomeStuff())
.subscribe([one, two] => {
//someActions
})
}
我想尝试做的是订阅 getCustomData()
并在 getCustomData
完成后调用 serviceTwo, serviceThree
。
ngOnInit(): void {
this.getCustomData();
}
getCustomData(): void {
forkJoin([
this.service.doOne(),
this.service.doTwo()
])
.pipe(doSomeStuff())
.subscribe([one, two] => {
this.getServiceTwoThreeData();
})
}
getServiceTwoThreeData() {
forkJoin([
this.serviceTwo.test(),
this.serviceThree.test()
])
.pipe(doSomeStuff())
.subscribe([test1, test2=> {
//someActions
})
}
您只需将服务二、服务三的 forkJoin 添加到一个方法中,然后从订阅中调用它。
如果我理解正确,你想做的解决方案可能是这样的:
ngOnInit(): void {
getCustomData()
.pipe(switchMap([one, two] => {
//someActions
return forkJoin([
this.serviceTwo.test(),
this.serviceThree.test()
])
}),
doSomeStuff()
).subscribe(([test1, test2])=> {
//someActions
})
}
getCustomData(): Observable<any>{
return forkJoin([
this.service.doOne(),
this.service.doTwo()
])
.pipe(doSomeStuff())
}
现在您正在等待 getCustomData 中的所有内容都已完成,您在管道中调用 switchMap 并在返回新的 Observable(带有测试方法的 forkJoin)之前执行您通常在 getCustomData 订阅中执行的操作。最后,您必须订阅所有内容,就像您已经订阅的那样。
查看 switchMap 了解更多信息。
如果我理解正确,你需要并行执行 this.service.doOne()
和 this.service.doTwo()
,等待两者完成,然后执行 serviceTwo
和 serviceThree
.
如果是这样,那我就这样
// getCustomData returns an Observable that represents the execution of
// doOne() and doTwo() in parallel followed by doSomeStuff()
// By the way, you have to make sure that doSomeStuff() returns a pipeable operator
// which accepts [resultOfDoOne, resultOfDoTwo] as input parameters
// It is important NOT TO SUBSCRIBE here, you just return the Observable
getCustomData() {
return forkJoin([
this.service.doOne(),
this.service.doTwo()
])
.pipe(doSomeStuff())
}
ngOnInit(): void {
// call getCustomData() to get the Observable
getCustomData()
// now you can pipe something into that Observable
.pipe(
// since you want to execute first getCustomData() and then serviceTwo
// and serviceThree, you have to concatenate the 2 Observable
concatMap(() => forkJoin([
this.serviceTwo.test(),
this.serviceThree.test()
])),
// now you do some stuff, again make sure that doSomeStuff() returns a
// pipeable operator that accepts resultOfServiceTwo and resultOfServiceThree
doSomeStuff()
)
// eventually you subscribe
.subscribe([test1, test2=> {
//someActions
})
}
在上面的实现中,请注意 doSomeStuff
应该做什么。
如果 doSomeStuff
只是实现了一个副作用,即它只是对上游通知的值做了一些事情,那么你可能应该看看 tap
运算符,但这很难猜到根据您显示的代码。
我有一个非常基本的问题。
我确实有一个包装 forkJoin
call
ngOnInit(): void {
getCustomData();
forkJoin([
this.serviceTwo.test(),
this.serviceThree.test()
])
.pipe(doSomeStuff())
.subscribe([test1, test2=> {
//someActions
})
}
getCustomData(): void {
forkJoin([
this.service.doOne(),
this.service.doTwo()
])
.pipe(doSomeStuff())
.subscribe([one, two] => {
//someActions
})
}
我想尝试做的是订阅 getCustomData()
并在 getCustomData
完成后调用 serviceTwo, serviceThree
。
ngOnInit(): void {
this.getCustomData();
}
getCustomData(): void {
forkJoin([
this.service.doOne(),
this.service.doTwo()
])
.pipe(doSomeStuff())
.subscribe([one, two] => {
this.getServiceTwoThreeData();
})
}
getServiceTwoThreeData() {
forkJoin([
this.serviceTwo.test(),
this.serviceThree.test()
])
.pipe(doSomeStuff())
.subscribe([test1, test2=> {
//someActions
})
}
您只需将服务二、服务三的 forkJoin 添加到一个方法中,然后从订阅中调用它。
如果我理解正确,你想做的解决方案可能是这样的:
ngOnInit(): void {
getCustomData()
.pipe(switchMap([one, two] => {
//someActions
return forkJoin([
this.serviceTwo.test(),
this.serviceThree.test()
])
}),
doSomeStuff()
).subscribe(([test1, test2])=> {
//someActions
})
}
getCustomData(): Observable<any>{
return forkJoin([
this.service.doOne(),
this.service.doTwo()
])
.pipe(doSomeStuff())
}
现在您正在等待 getCustomData 中的所有内容都已完成,您在管道中调用 switchMap 并在返回新的 Observable(带有测试方法的 forkJoin)之前执行您通常在 getCustomData 订阅中执行的操作。最后,您必须订阅所有内容,就像您已经订阅的那样。
查看 switchMap 了解更多信息。
如果我理解正确,你需要并行执行 this.service.doOne()
和 this.service.doTwo()
,等待两者完成,然后执行 serviceTwo
和 serviceThree
.
如果是这样,那我就这样
// getCustomData returns an Observable that represents the execution of
// doOne() and doTwo() in parallel followed by doSomeStuff()
// By the way, you have to make sure that doSomeStuff() returns a pipeable operator
// which accepts [resultOfDoOne, resultOfDoTwo] as input parameters
// It is important NOT TO SUBSCRIBE here, you just return the Observable
getCustomData() {
return forkJoin([
this.service.doOne(),
this.service.doTwo()
])
.pipe(doSomeStuff())
}
ngOnInit(): void {
// call getCustomData() to get the Observable
getCustomData()
// now you can pipe something into that Observable
.pipe(
// since you want to execute first getCustomData() and then serviceTwo
// and serviceThree, you have to concatenate the 2 Observable
concatMap(() => forkJoin([
this.serviceTwo.test(),
this.serviceThree.test()
])),
// now you do some stuff, again make sure that doSomeStuff() returns a
// pipeable operator that accepts resultOfServiceTwo and resultOfServiceThree
doSomeStuff()
)
// eventually you subscribe
.subscribe([test1, test2=> {
//someActions
})
}
在上面的实现中,请注意 doSomeStuff
应该做什么。
如果 doSomeStuff
只是实现了一个副作用,即它只是对上游通知的值做了一些事情,那么你可能应该看看 tap
运算符,但这很难猜到根据您显示的代码。