I am really new to funcitonal paradigm and I have a hard time understanding what is going on with the following snippet.
const countDown = (val, cb, delay = 1000) => {
cb(val, delay);
return (val > 0) ? setTimeout(() => countDown(val - 1, cb, delay), delay) : val;
}
countDown(5, console.log, 100)
Why the following doesn't work, given the fact that setTimeout takes a function as an argument?
const countDown = (val, cb, delay = 1000) => {
cb(val, delay);
return (val > 0) ? setTimeout(countDown(val - 1, cb, delay), delay) : val;
}
countDown(5, console.log, 100)
countDown. That's why the arrow function exists on the first example.countDownis a function, butcountDown(val - 1, cb, delay)is not.