Last updated Jun 7, 2021

Share Reply - RXJS Operator

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

Share Reply - RXJS Operator

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.

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

0 comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

No comments yet — be the first to start the discussion.

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.