Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Filter Events with RxJS Operators take, first, and skip


    There are more operators in the filtering category besides filter(). This lesson will teach how take(), first(), and skip() are simply operators to ignore or pass a certain amount of events from the source Observable.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    We just saw filter, which is in the category of filtering operators, which means there are several ways of ignoring events, not just with the filter operator. We're going to see next the operators take, first, and skip.

    We're going to go back to looking at this observable of numbers, but let's say this time I want to allow only the first five numbers and then stop. Interval is an infinite observable, and I really just want the first five numbers.

    It would be quite cumbersome to do that with filter, because then you would need to keep track of how many events were sent already and returned true or false accordingly. That's why we use take instead.

    Take allows us to specify a number here, which means how many events we want to take or pass. Then, after that, we're going to complete the observable.

    Here, we're just going to say five, and then the observable output at the bottom will have, at most, five events. After that, it just completes.

    Let's see how take works. We say take five. It will take only five events from this interval observable called foo. After that, it's going to complete. Every one second, it delivers a value. Once it hits four, it's done.

    There's a shortcut. It's quite common to do take one, which means take the first number and then stop. Take one is quite common, and that's why we have an operator that does exactly the same thing as take one. It's called first. What it does, it lets the first event pass, and then it completes. Like that.

    In the other case, let's say that we have this observable of numbers, and we want to ignore the first five numbers. Instead of passing them, we want to ignore them. That's the use case for skip, where we also specify a number of how many events should we ignore in the beginning.

    Let's say we want to ignore the first five. That means that it's going to ignore zero, one, two, three, and four. Then, after that, it starts passing five, six, and seven. I'm just going to make this interval be faster, otherwise we need to wait six seconds. Then, I'm going to call skip five.

    When we run this, we see five, six, seven, etc. This is an infinite observable.

    Just remember that you use take in order to ignore the rest after the fifth, or you use skip in order to ignore the first five.