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 `lift` to Connect a `source` to a `subscriber` in RxJS

    John LindquistJohn Lindquist

    The lift method on each source hides away the internals of RxJS so you can simply connect a source to the subscriber you're working with. The lift method take an object with a call function with subscriber and source arguments, then it's up to you how you want to connect them together.



    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




    Instructor: I'm going to grab this entire function, cut it out, and call it, just double, and assign double to that, and pass double into here. Now, everything is working as expected.

    We get 1, 2, 3, 4, 5 inside of our subscriber which is passed along to the destination, and doubled, so our destination is here, 2, 4, 6, 8, 10. You can see this observable is piping through the double function and properly passing those values down to the subscriber.

    Now, inside of here, where I said don't do it this way. I said that, because there is a convenience function called source.lift. If I cut out this operator object, and instead of returning this block, I'm going to return source.lift and paste in that operator object.

    You can see we're back to where we were, where we have 2, 4, 6, 8, 10 and 1, 2, 3, 4, 5, but now we don't have to jump through the hoops of creating the observable, assigning the source, assigning the operator.

    All we have to do is a say source lift, and pass in as object that has a call on it. Then, when subscribe is invoked, it will check for this object and set up all of that stuff under the hood for us, so the before looks like this where we created, assign the source, assign the operator, and return it which is the exact same thing as just source lift.

    Then, passing in that operator array object that has call on it. For the call takes the subscriber and the source and wires it up for you, and gives you that opportunity to pass in your custom subscriber.