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
1×
Become a member
to unlock all features

Level Up!

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

Autoplay

    Create Operators from Existing Operators in RxJS

    John LindquistJohn Lindquist
    rxjsRxJS
    ^6.0.0

    The most common scenario for creating custom operators is to reuse the built-in operators shipped with RxJS. You'll find yourself re-using map, filter, and others will solve most of the problems you come across.

    Code

    Code

    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
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: The proper way of implementing multiply, rather than going through the whole process of creating your own subscriber, is probably to delete all of this and import map from RxJS operators. Then map is a function which already takes the source. I can just replace this function with map.

    Map takes a value. That's the value coming into the internal next, in our subscriber. Then I'll say, "value times number." You can see we get the same results over here.

    Just to prove this is working, I'll do times two. You can see these all change to a multiple of two. I'll change that back to number. We have our multiples of three and four being logged out here.

    The practical way of creating new operators is to import ones that you know work, and have all of the error handling and proper subscribers and unsubscribing built in, and then just pass in the arguments that you want to, and return that function.