RxJS: Pipelining Lettable Operators

Image for post
Image for post
Photo by David Rangel on Unsplash

Earlier this week, a TC39 proposal for a pipeline operator moved to stage-1. If the proposal is eventually accepted and included in the ECMAScript standard — it has a long way to go — it will offer a new syntax for lettable operators.

The proposed pipeline operator is |>. It’s a binary operator; the operand to its left is a value and the operand to its right is a function. The pipeline operator calls the function, passing the value as an argument, and returns the function’s result.

That is, 64 |> Math.sqrt is equivalent to Math.sqrt(64).

Using the pipeline operator, multiple functions can be pipelined, like this:

The lettable operators introduced in the RxJS 5.5.0 beta are higher-order functions. They return functions that receive and return observables. As such, they can be used with the proposed pipeline operator, like this:

Whether or not the pipeline proposal will navigate its way through the TC39 process to be included in the ECMAScript standard remains to be seen.

However, if you like to live at the bleeding edge, there is a Babel plugin for the proposal: babel-plugin-transform-pipeline. It does have some issues with recent Babel packages, but with the proposal at stage-1, it should not be long before an up-to-date plugin is included in Babel’s stage-1 preset.

Regarding TypeScript, the pipeline proposal won’t be considered until it reaches stage-2 (or maybe stage-3).

This post is also published on my personal blog: ncjamieson.com.

RxJS core team member; front-end developer; mentor; speaker; open-source contributor

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store