Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
¿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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda