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

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

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

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 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