I have pretty basic question.
I do have a method which wraps 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
})
}
And what I would like to try to do is subscribe to getCustomData() and do calls to serviceTwo, serviceThree after getCustomData is finished.
If I understood correctly what you're trying to do the solution could be like this:
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())
}
So now you're waiting that everything is finished in getCustomData, you call switchMap in the pipeline and before returning the new Observable (the forkJoin with test methods) you do what you usually do in the getCustomData subscribe. Lastely you have to just subscribe to the whole thing, as you already do.
Check switchMap for more info.
If I understand the problem right, you need to do perform this.service.doOne() and this.service.doTwo() in parallel, wait for the completion of both and THEN perform serviceTwo and serviceThree.
If this is the case, then I would proceed like this
// 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
})
}
In the above implementation be careful about what doSomeStuff should do.
If doSomeStuff just implements a side effect, i.e. it just does something with the values notified by the upstream, then you probably should look at the tap operator, but this is difficult to guess from the code you have shown.
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
})
}
You can simply add your service two, service three forkJoin to a method and call it from subscribe.