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

151
Vistas
How can I retry with a delay in RxJs without using the deprecated retryWhen?

I want to retry an observable chain with a delay (say, 2 seconds).

There are some similar questions answered with retryWhen. But retryWhen seems deprecated and I don't want to use it.

  • The delay and retry is applicable only if the first try fails
  • If all the retries failed, I want to respond with default data.

The following code works, without adding the delay, but how can I add delay into this chain?

of('trigger')
  .pipe(
    switchMap(() => fetchData()),
    retry(5),
    catchError(() => of(returnThisIfRetriesAreFailed))
  )
  .subscribe(console.log);

StackBlitz entry

I tried adding delay(2000), but it does not seem to be working.

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

0

I am not sure what you mean by

The delay and retry is applicable only if the first try fails

I couldn't find any condition in the source code that would limit the delay option to be applicable only to the first failed attempt.

So you should be able to pass as a delay either a fixed number or an Observable. For example:

of('trigger')
  .pipe(
    switchMap(() => fetchData()),
    retry({count: 5, delay: 2000}),
    catchError(() => of(returnThisIfRetriesAreFailed))
  )
  .subscribe(console.log);

If you meant you only want to delay on the first retry, then you could try something like this:

of('trigger')
  .pipe(
    switchMap(() => fetchData()),
    retry({count: 5, delay: (_, retryCount) => retryCount === 1 ? timer(2000) : of({})}),
    catchError(() => of(returnThisIfRetriesAreFailed))
  )
  .subscribe(console.log);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use:

of('trigger').pipe(
    switchMap(() => fetchData()),
    catchError((err, caught) => caught.pipe(
                    repeat({ count: 1, delay: 2000 }),
                    catchError(() => of(returnThisIfRetriesAreFailed))
              ))
)
.subscribe(console.log);
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