1
const waterfall = (...functions) => (callback, ...args) =>
  functions.reduceRight(
    (composition, fn) => (...results) => fn(composition, ...results),
    callback
  )(...args);

const randInt = max => Math.floor(Math.random() * max)

const add5 = (callback, x) => {
  setTimeout(callback, randInt(1000), x + 5);
};
const mult3 = (callback, x) => {
  setTimeout(callback, randInt(1000), x * 3);
};
const sub2 = (callback, x) => {
  setTimeout(callback, randInt(1000), x - 2);
};
const split = (callback, x) => {
  setTimeout(callback, randInt(1000), x, x);
};
const add = (callback, x, y) => {
  setTimeout(callback, randInt(1000), x + y);
};
const div4 = (callback, x) => {
  setTimeout(callback, randInt(1000), x / 4);
};

const computation = waterfall(add5, mult3, sub2, split, add, div4);
computation(console.log, 5) // -> 14

// same as:

const computation2 = (input, callback) => {
  const f6 = x=> div4(callback, x);
  const f5 = (x, y) => add(f6, x, y);
  const f4 = x => split(f5, x);
  const f3 = x => sub2(f4, x);
  const f2 = x => mult3(f3, x);
  add5(f2, input);
}

I understand the workings around .reduceRight(), however the sample code on running a list of asynchronous function is quite confusing to me as from the looks of it it seems like it is not following the syntax provided by the doc but somehow it worked. Where did the composition comes from? What about the ...args, is it right to assume the ...args is 5 from computation(console.log, 5)? Also I'm pretty confused by the purpose of why is there an invoked paranthesis (...args) on const waterfall?

Also one side question, is it necessary to read the entire MDN doc to fully understand JavaScript? I'm planning to read the entire doc but somehow I have been reading for almost weeks and I see no end to it.

MDN doc source here

VLAZ
  • 26,331
  • 9
  • 49
  • 67
YGOPRO
  • 29
  • 5
  • 4
    On your question about reading all of MDN, I would strongly advise against it. It's like trying to learn to drive by reading the car manual. If you're looking for good reading, I would recommend starting with something like [You Don't Know JS](https://github.com/jumaschion/You-Dont-Know-JS-1). You can read it for free on Github, you don't have to buy the book. – Etheryte Jun 18 '22 at 21:28
  • 1
    composition's just an argument like any other, what do you mean "represent"? The ellipses are covered in e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax – jonrsharpe Jun 18 '22 at 21:31
  • In general see https://stackoverflow.com/q/9549780/3001761 – jonrsharpe Jun 18 '22 at 21:35
  • @Etheryte oh thanks for the suggestion, I will definitely look that up. – YGOPRO Jun 18 '22 at 21:35
  • @jonrsharpe I understand that is the spread syntax/rest parameter being used, I just got bamboozled by how `(...args)` appeared at the end of the `const waterfall`. Is it right to assume that is an invoked parenthesis? – YGOPRO Jun 18 '22 at 21:37
  • `(...args)` there is a call to whatever `reduceRight` returns, if that's what you mean by "an invoked parenthesis". – jonrsharpe Jun 18 '22 at 21:39

0 Answers0