I\'ve taken a look at a few questions on recursion in promises and am confused on how to implement them properly:
Try not to use shared mutable state in your functions (especially when they are asynchronous). You are using window.i but anything can change that value, this is not needed because the i value is only used in your function as a counter:
const later = (milliseconds,value) =>
new Promise(
resolve=>
setTimeout(
()=>resolve(value),
milliseconds
)
);
const countTo = toWhat => {
const recur = counter =>
later(1000,counter)
.then(
i=>{
console.log(`i is now: ${i}`);
return (iconsole.log(`i ended up at: ${i}`)
);