The pipe operator, I must admit, is something I’ve been wishing to have since I discovered it on Elixir.

For example, this :

Math.max(
    ...Object.values(
        JSON.parse(
            '{"a":84,"b":91,"c":52,"d":26,"e":78,"f":19,"g":96,"h":57}'
        ) // -> {a: 84, b: 91, c: 52, d: 26, e: 78, …}
    ) // -> [84, 91, 52, 26, 78, 19, 96, 57]
) // -> 96

Could turn into this :

'{"a":84,"b":91,"c":52,"d":26,"e":78,"f":19,"g":96,"h":57}'
|> JSON.parse(%) // -> {a: 84, b: 91, c: 52, d: 26, e: 78, …}
|> Object.values(%) // -> [84, 91, 52, 26, 78, 19, 96, 57]
|> Math.max(...%) // -> 96

As per the proposed specification for a JS pipe operator.(1)

I liked this so much that I made my own imitating pipe function as well.

But, 3 months later, I realized I could do it natively (even if a little more verbosely) like this :

await Promise
    .resolve('{"a":84,"b":91,"c":52,"d":26,"e":78,"f":19,"g":96,"h":57}')
    .then(JSON.parse) // -> {a: 84, b: 91, c: 52, d: 26, e: 78, …}
    .then(Object.values) // -> [84, 91, 52, 26, 78, 19, 96, 57]
    .then(_ => Math.max(..._)) // -> 96

As I remembered how the following :

const response = await (await fetch()).json();
doSomethingWith(response);

Used to look like the following :

fetch()
    .then(response => response.json())
    .then(response => doSomethingWith(response));

Making me rediscover the notion of promise chaining, that I was using only for fetch and without really knowing about it, at the time.

And while researching this, I noticed someone else also mentioned this possible pattern in passing.


(1) Proposed specification for a JS pipe operator, which you can try out on babeljs.io/repl by adding the @babel/plugin-proposal-pipeline-operator plugin.