I am new to javascript and reading about closures currently. I was trying to implement an example to better understand the concept.
This is the snippet:
function getValue() {
let a = 0
const runner = async () => {
a += 1
}
setInterval(runner, 100)
return () => {
return a
}
}
let value = getValue()
console.log(value()) -----> logging statement 1
// some long operation
console.log(value()) -----> logging statement 2
However, the output that I see is:
1
1
I expected the two logging statements to output two different values. My understanding is that once getValue() is called, the runner function would execute at regular intervals. The variable a would get updated and hence the 2nd console.log should have printed an incremented value of a.