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

163
Vistas
Avoiding nesting Observables ( mergeMap on complete )

Got this pseudo code for a scraping service that first explores the pagination urls then the subpage urls on the scraped "parent" website

const pagination = [["url-1", "url-2", "url-3", "url-4"], ["url-5"]];
const timeInterval = 2;

sendRequestsInIntervals(pagination, timeInterval )
  .pipe()
  .subscribe({
    next(x) {
      console.log(x);
    },
    error(err) {
      console.error(err);
    },
    complete() {
      console.log("done");
      sendRequestsInIntervals(resDataArray, timeInterval ).subscribe({
        next(x) {
          console.log(x);
        },
      });
    },
  });

I wanna avoid nesting as its an incorrect way to use observables

Is there a way to convert this to something like this:

sendRequestsInIntervals(pagination, timeInterval )
  .pipe(
    waitUntilCompletes()
    mergeMap((resDataArray) => {
      return sendRequestsInIntervals(resDataArray, timeInterval );
    })
  )
  .subscribe({
    next(x) {
      console.log(x);
    },
    error(err) {
      console.error(err);
    },
    complete() {
      console.log("done");
    },
  });

Added a pseudo function called waitUntilCompletes()

Is there such a thing in rxJS that it makes the observable in the mergeMap wait before runing until the previous observable is completed?

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

0

the last operator will ignore all emissions until the final one upon completion:

sendRequestsInIntervals(pagination, timeInterval )
  .pipe(
    last(),
    mergeMap((resDataArray) => { // resDataArray is only the final emitted value from sendRequestsInIntervals() 
      return sendRequestsInIntervals(resDataArray, timeInterval );
    })
  )
  .subscribe({
    next(x) {
      console.log(x);
    },
    error(err) {
      console.error(err);
    },
    complete() {
      console.log("done");
    },
  });

or you can use reduce to gather the emissions and emit once complete:

sendRequestsInIntervals(pagination, timeInterval )
  .pipe(
    // this will gather all outter emissions into an array and emit once outter completes
    reduce((acc, val) => acc.concat(val), []),
    mergeMap((resDataArray) => { // resDataArray is all values emitted from sendRequestsInIntervals() in an array
      return sendRequestsInIntervals(resDataArray, timeInterval );
    })
  )
  .subscribe({
    next(x) {
      console.log(x);
    },
    error(err) {
      console.error(err);
    },
    complete() {
      console.log("done");
    },
  });
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