Tengo una pregunta bastante básica. Tengo un método que envuelve la llamada forkJoin
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 }) } Y lo que me gustaría intentar hacer es suscribirme a getCustomData() y hacer llamadas a serviceTwo, serviceThree después de que getCustomData .
Si entendí correctamente lo que estás tratando de hacer, la solución podría ser así:
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()) }Así que ahora estás esperando que todo esté terminado en getCustomData, llamas a switchMap en el pipeline y antes de devolver el nuevo Observable (el forkJoin con métodos de prueba) haces lo que normalmente haces en getCustomData subscribe. Por último, solo tienes que suscribirte a todo, como ya lo haces.
Consulte switchMap para obtener más información.
Si entiendo bien el problema, debe realizar this.service.doOne() y this.service.doTwo() en paralelo, esperar a que se completen ambos y LUEGO realizar serviceTwo y serviceThree .
Si este es el caso, entonces procedería así.
// 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 }) } En la implementación anterior, tenga cuidado con lo que debe hacer doSomeStuff .
Si doSomeStuff solo implementa un efecto secundario, es decir, solo hace algo con los valores notificados por el upstream, entonces probablemente debería mirar el operador tap , pero esto es difícil de adivinar a partir del código que ha mostrado.
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 }) }Simplemente puede agregar su servicio dos, servicio tres forkJoin a un método y llamarlo desde suscribirse.