In this tutorial, we will learn about the Retry, RetryWhen, Scan, and Delay operators of RXJS.

Introduction to Retry, RetryWhen, Scan, and Delay Operator of RXJS:

Retry Operator

Retry used for re-subscriber observable in cases observable not getting subscriber first time.

Example:

fetchUsers() {
    console.log('Fetching Data...');
    this.http
      .get("https://jsonplaceholder.typicode.com/users")
      .pipe(
        retry(3) 
      )
      .subscribe(
        (res) => {
           console.log('Data Fetched');
        },
        (err) => {
          console.log('Problem Fetching Data');
         
        }
      );
  }

The above example will retry the operation three times.

Also note that The Retry operator retries the operation immediately.

If your Retry without any argument, it will retry indefinitely

retryWhen Operator

with using the RxJS retryWhen operator you can retry the request if specified conditions are met

Example:

fetchUsers() {
    console.log('Fetching Data...');
    this.http
      .get("https://jsonplaceholder.typicode.com/users")
      .pipe(
        retryWhen((err) =>
          err.pipe(
            tap(() => console.log("Retrying... "))))
          )
        )
      )
      .subscribe(
        (res) => {
          console.log('Data Fetched');
        },
        (err) => {
          console.log('Problem Fetching Data');
        }
      );
  }

Scan and Delay Operator

With using Delay operator we can delay retry attempts and with using the Scan operator we can limit retry attempts

Example:

fetchUsers() {
    console.log('Fetching Data...');
   
    this.http
      .get("https://jsonplaceholder.typicode.com/users")
      .pipe(
        //Will try for Every 3 sec
        retryWhen((err) =>
          err.pipe(
            delay(3000),
            scan((retryCount) => {
              if (retryCount >= 5) {
                throw err;
              } else {
                retryCount = retryCount + 1;
                console.log(retryCount);
                this.status = "Rerying Attempt #" + retryCount;
                return retryCount;
              }
            }, 0)
          )
        )
      )
      .subscribe(
        (res) => {
             console.log('Data Fetched');
        },
        (err) => {
          console.log('Problem Fetching Data');
        }
      );
  }

The Above example will retry operation at every 3 seconds for five time

Topics covered:

Found this article helpful?

TutsCoder tutorials are free and ad-light — supported by readers like you. Buy me a coffee (or two ☕☕) as a token of appreciation and help keep Angular & Node.js content coming!

One-time. No subscription. 100% optional. 🙏 Every coffee counts!

Leave a Comment

Your email will not be published. Spam-free zone. ✌️

Available for Projects

Need Help With Your
Angular or Node.js Project?

7+ years of MEAN Stack experience. I build scalable Angular 21 apps, Node.js APIs, and SaaS products — delivered on time, every time.

7+ Years MEAN Stack Angular 21 + Nx Expert 20+ Projects Delivered Remote / Freelance