6
I will exemplify with codes.
I have an asynchronous function called delay
which receives a time in seconds and which returns a Promise. It serves to provide a waiting time in seconds:
// padrão de 1 segundo
function delay (time = 1) {
return new Promise((resolve) => {
setTimeout(resolve, time * 1_000) // resolve depois de X segundos
})
}
Now I’m going to create a clock function clock
which will display the local time ( toLocaleTimeString
) at each delay time (time
) defined in function delay
, and when this is resolved, I will display a log in the console of the current time. Then recursively invoke the function clock
and return it:
function clock () {
return delay(1).then(() => {
// exibe a hora
console.log(new Date().toLocaleTimeString())
// inicia a recursão
return clock()
})
}
Let’s run our clock:
// padrão de 1 segundo
function delay (time = 1) {
return new Promise((resolve) => {
setTimeout(resolve, time * 1_000) // resolve depois de X segundos
})
}
function clock () {
return delay(1).then(() => {
// exibe a hora
console.log(new Date().toLocaleTimeString())
// inicia a recursão
return clock()
})
}
// bootstrap
clock()
Using async/await
:
// padrão de 1 segundo
function delay (time = 1) {
return new Promise((resolve) => {
setTimeout(resolve, time * 1_000) // resolve depois de X segundos
})
}
async function clock () {
await delay(1)
// exibe a hora
console.log(new Date().toLocaleTimeString())
// inicia a recursão
return clock()
}
// bootstrap
clock()
Note that in both cases, the code works normally and the clock shows the updated time every second. However, the fact that I have a function that returns a Promise (returns delay
and this returns an object Promise
) and this Promise activates recursion, generates a current of Promises. This should be avoided!
- Why?
- What problem do I solve by interrupting the current of Promises caused by recursion in
clock
?
I found nothing related. If you found, feel free to signal. I think the question can contribute to the community :)
– Cmte Cardeal
I found the question very interesting, especially for the +1 examples
– Ricardo Pontual
I’ve never really seen this, but I suspect you already know the answer. I want a spoiler! I suppose it’s because of the gradual buildup of memory in call stack (I tried it here and it kind of happened), but I’m not sure... Anyway, there’s no gain in using recursion there. A simple
while
would already solve.– Luiz Felipe
@Luizfelipe is that same. It has to do with memory Leak
– Cmte Cardeal
Like Maniero says, working is different than being right. I question the need to use recursion in the case of the question, but I think it was an example to situate the problem. I follow the premise that, since leading Javascript Engines don’t even perform TCO, it’s almost never really worth using Javascript recursion. And as there is the equivalence between recursion and "common" ties, these are always preferable when the minimum performance (in this case mainly memory saving) is an important factor.
– Luiz Felipe