Last updated Jun 7, 2021

Take, TakeLast, TakeUntil Operator- RXJS

In this tutorial, we will learn about take, takeLast, and takeUntill operator of RXJS.

Take, TakeLast, TakeUntil Operator- RXJS

In this tutorial, we will learn about take, takeLast, and takeUntill operator of RXJS.

Take Operator:

  The take operator is a Filtering Operator. Filtering Operators allows you to select how and when to accept values emitted from Observables.  

It allows you to specify a maximum number of values you want to receive from an Observable

Take() Emits only the first count values emitted by the source Observable.

Using take we can take only data which we required from observable

randomNames = ["jigar", "Rahul", "Seema", "Aadi", "Megh"];

const nameSource = from(this.randomNames);

nameSource.pipe(take(4)).subscribe((res) => {
      console.log(res);
    });

Output:

jigar
Rahul
Seema
Aadi

Use Cases:

  It can be used in situations where we want to limit how many user-produced events (fromEvent) we want to handle, for example, the first time the user clicks in our app

takeLast Operator

takeLast() Emits only the last count values emitted by the source Observable.

randomNames = ["jigar", "Rahul", "Seema", "Aadi", "Megh"];

const nameSource = from(this.randomNames);
 
nameSource.pipe(takeLast(4)).subscribe((res) => {
      console.log(res);
    });

Output:

Rahul
Seema
Aadi
Megh

TakeUntil Operator

takeUntil Emits the values emitted by the source Observable until a notifier Observable emits a value.

Normally we can say takeUntill operator is based on condition and Takes condition as observable

For Example, we want a timer that will stop after some specific seconds, lets see how we can do this using takeUntill Operator:

const source = interval(1000);
    //Will Stop after 6 sec
    let condition = timer(6000);

    source
      .pipe(
        map((res) => "Number " + res),
        takeUntil(condition)
      )
      .subscribe((res) => {
        console.log(res);
      });

Output:

1
2
3
5
6

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

Comments

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

Loading comments…

Join the discussion

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