Create Operators from Existing Operators in RxJS

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 6 years ago

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.

Instructor: [00:00] 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.

[00:20] 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.

[00:31] 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.

[00:45] 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.

Ian
Ian
~ 6 years ago

Can't get this to work as it's complaining about the value on the right hand side of the operator: "[ts] The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type."

Markdown supported.
Become a member to join the discussionEnroll Today