In this tutorial, we will learn about the Share Reply operator of RXJS.

Share Reply

By using the shareReply() operator we can Avoid duplicate requests for the same data.

Suppose we have one API request which returns us all users data, now we want to use the same data pf users with different filters, without calling API again, let's how we can do that using Share Reply Operator:

Example:

url = "https://jsonplaceholder.typicode.com/users";

users: Observable<any>;
users2: Observable<any>;
users3: Observable<any>;

ngOnInit() {
    this.getData();
}
getData() {
    this.users = this.http.get(this.url).pipe(shareReplay());

    this.users2 = this.users.pipe(
      map((res) =>
        res.filter((data) => {
          return data["id"] == "2";
        })
      )
    );

    this.users3 = this.users.pipe(
      map((res) =>
        res.filter((data) => {
          return data["id"] == "3";
        })
      )
    );
  }

The above example will make only one request to API and will share the result to another observable, which avoids making extra API calls and make performance improvements.

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