Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

225
Vistas
¿Puedo suscribirme al método que está ejecutando forkJoin inside?

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 .

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

 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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda