const compose = (...fns) =>
fns.reverse().reduce((prevFn, nextFn) =>
value => nextFn(prevFn(value)),
value => value
);
Create the function, composed of three others:
const example = compose(
val => { console.log(1); return `1<${val}>`; },
val => { console.log(2); return `2<${val}>`; },
val => { console.log(3); return `3<${val}>`; }
);
Call the function:
x('hello')
Console output is:
3
2
1
"1<2<3<hello>>>"