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.


    Use Marble Diagrams to Understand RxJS Operators

    André StaltzAndré Staltz

    There are many operators available, and in order to understand them we need to have a simple way of communicating how they transform a source Observable into a result Observable over time. To show this we use Marble Diagrams to easily explain what is happening. Throughout this course we are going to see the so-called marble diagrams.



    Become a Member to view code

    You must be a 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




    As we're going to see in upcoming lessons, there are many operators available. In order to understand them, we need to have a simple way of communicating how they transform a source observable into a result observable over time.

    Instead of using sentences in English to explain how they work, a diagram often works best. Throughout this course, we're going to see the so-called marble diagrams. They're really handy and easy to draw. For instance, here's how they look like in ASCII form inside these comments.

    We're going to draw the marble diagram for one observable, this observable interval of one second. They always look like this. They start with these dashes to indicate time, where time starts from the left side and it progresses towards the right.

    If we have lowercase characters like these, they will represent values delivered to the If they are numbers, they are also values. We're going to consider them to represent values. If the character is a pipe character, that represents the completed notification. If it's the uppercase X, then that represents an error.

    That's everything that an observable can deliver -- values, completed, or error. Values would be in the range of zero and nine, those numbers, or A to Z as letters. Then, completed is the pipe and X is the error.

    Let's draw the observable marble diagram for interval. Let's consider one second to be four dashes. This is really arbitrary. We're just choosing an amount of dashes to mean one second. After one second, delivers the value zero, then delivers one, then delivers two, three, four, five. Then, we can also use ellipses here to indicate that this just continues indefinitely.

    There's a special case when we have synchronous values being delivered. For instance, if we use the observable of, then these values will be immediately delivered, so synchronously, when we subscribe to that observable.

    That is different to represent, because we use the parentheses to indicate that all of the values here in this parentheses group will be delivered synchronously. In case we had this special observable that after delivering one, two, three, four, it would deliver five after one second and six after another second, then we would represent that like this.

    Just to indicate that these happened synchronously, but five did not happen in that synchronous execution. It happened after a while. Just keep in mind that when we use parentheses, we mean that all of these are actually grouped inside one instantaneous moment.

    Marble diagrams are also handy for representing operators. We represent operators by using one marble diagram for an observable at the top for the source. Then, we use text here just to indicate what is the operator that we're using, and we indicate the result observable at the bottom here.

    Let's see how that would look like if we have interval of one second. If that is foo, as our source, let's even write here. Foo, we can do that, and bar is the result. Let's say we're multiplying by two. Then, after one second, we deliver zero on foo, two, three, etc. Zero multiplied by two is zero, two, four, six, and etc.

    Just keep in mind that at the top, we have the source observable. In the middle, we have the operator and its arguments. At the bottom, we have the result observable.

    This is how we will indicate most of our marble diagrams for operators in order to understand better how they work.