Skip to content

Instantly share code, notes, and snippets.

@nkhil
Created February 27, 2020 17:45
Show Gist options
  • Save nkhil/3f3f8c2319488d2ea6e66b8ad9e042d9 to your computer and use it in GitHub Desktop.
Save nkhil/3f3f8c2319488d2ea6e66b8ad9e042d9 to your computer and use it in GitHub Desktop.

Example of functional piping

  const pipeAsync = (...fns) => async (value) => {
    console.log('fns', fns)
    return await fns.reduce((sum, fn) => {
      if (typeof fn !== 'function') {
        throw new TypeError('fn is not a function');
      }
      return Promise.resolve(sum).then(fn);
    }, value);
  }

  const add = (numArr) => {
    return numArr.reduce((acc, num) => acc + num, 0);
  }

  const multiplyByTen = (num) => {
    return num * 10;
  }

  const addOne = (num) => {
    return num + 1;
  }

  (async () => {
    /* */
    const result = await pipeAsync(add, multiplyByTen, addOne)([1, 2, 3, 4, 5]);
    console.log('-'.repeat(50), 'result', result);
  })();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment