Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!