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

137
Vistas
Wait in nested observables

I'm new to Observables; I have to convert a nested for loop with Observables and, when a condition is meet, wait for a certain amount of time.
I was able to convert the for-loop with Observables, but I don't reach to achieve the "pause" of the flow of 5 seconds after the globalCounter variable value is set to 5.
How to do it? Thanks.

import { map, Observable, Subject } from "rxjs";

const array1 = ["Alfa", "Beta", "Gamma"];
const array2 = ["Laura", "Valentina", "Cristina"];
const array3 = ["Red", "Green", "Blue"];

const mySubject = new Subject<number>();
let globalCounter = 0;

const myObservable1$ = new Observable<string>((observer1) => {
  array1.map((u) => {
    observer1.next(u);
  });
});

const pipeline1 = myObservable1$
  .pipe(
    map(async (value1) => {
      const return2 = await fObservable2(value1);
      return2.subscribe({});
    })
  )
  .subscribe();

async function fObservable2(value1: string) {
  return new Observable<string>((observer2) => {
    array2.map(async (d) => {
      const valueToPassTofObservable3 = value1 + " --- " + d;
      const return3 = await fObservable3(valueToPassTofObservable3);
      return3.subscribe({
        next: async (value3) => {
          console.log(
            `OBSERVABLE2 - value3: ${value3} - globalCounter: ${globalCounter}`
          );
        },
      });
    });
  });
}


async function fObservable3(value2: string) {
  let toBeReturn3;
  if (globalCounter === 5) {
    await sleep();
  }
  return new Observable<string>((observer3) => {
    array3.forEach((t) => {
      toBeReturn3 = t + " --- " + value2;
      observer3.next(toBeReturn3);
      mySubject.next(globalCounter++);
    });
  });


}

function sleep() {
  return new Promise((resolve) => {
    setInterval(() => {
      globalCounter = 0;
      resolve(true);
    }, 5000);
  });
}

about 4 years ago · Juan Pablo Isaza
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