DEV Community

Discussion on: JS Functional Concepts: Pipe and Compose

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

It's your opinion and when you use it in your projects, it will be your code, so use the style you prefer or feel more comfortable with, it's totally OK! 😁

Edit: I wrote the post in different days (one bit at a time) and just realized I had been using different wording for the references on those functions

So for this one:

const pipe = (...functions) => 
  (initialArg) => {
    return functions.reduce((currentValue, currentFunction) => {
      return currentFunction(currentValue);
    }, initialArg);
  };
Enter fullscreen mode Exit fullscreen mode

we could save few keyboard clicks by coding it like this:

const pipe = (...functions) => (initialArg) => 
  functions.reduce((currentValue, currentFunction) => {
      return currentFunction(currentValue);
    }, initialArg);
Enter fullscreen mode Exit fullscreen mode

Just like this in the last one:

export const pipeAsync: any =
  (...fns: Promise<Function>[]) =>
  (initialArg: any) =>
    fns.reduce((currentFunc: Promise<Function>, func: Function | Promise<Function> | any) => currentFunc.then(func), Promise.resolve(initialArg));
Enter fullscreen mode Exit fullscreen mode

Which is probably more... understandable?

Let me know, if it helps I can update the post! 😁

Some comments have been hidden by the post's author - find out more