This is a.js
const a = require("./b");
setInterval(function(){console.log(a)},1000);
This is b.js
let a = 1;
async function hahaha(){
while(true){
await sleep(1000);
a += 1;
console.log(a)
}
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms))
}
hahaha()
module.exports = a;
My desired output is this:
2
1
3
2
4
3
5
4
.
.
.
But what I get is this:
2
1
3
1
4
1
5
1
.
.
.
I want to get an updated value of a when I call it every second while a is still in the process of another function hahaha. Is it possible to get updated values, which is not returned from a function, but in the process? I tried to google it, but I don't know which keyword to use. I feel so newbie now.