I would like to call a function in NodeJS which checks Date.Now continuously if a flag is set to true.
I cannot use setInterval because it is too inaccurate.
My function works fine, but I want to call the function without blocking the whole code, so I thought an async method would do the job, but it isn't:
var bufferLogRunning = true;
var dn_counter;
var masterbuffer_cache_log = [];
async function f() {
let promise = new Promise((resolve, reject) => {
console.log('FUNCION CALLED')
var start = Date.now();
while (bufferLogRunning == true) {
if (dn_counter != Date.now() - start && dn_counter != undefined) {
masterbuffer_cache_log.push({
"millis": dn_counter,
"buffer": "masterbuffer_cache"
})
}
dn_counter = Date.now() - start
if (Date.now() - start >= 4000) {
bufferLogRunning = false;
}
}
console.log('DONE')
resolve("done!")
});
let result = await promise;
}
console.log('FIRST')
f();
console.log('SECOND')
How do I execute console.log('SECOND') while f() is running in background?
I cannot use setInterval because it is too inaccurate.even if you use an interval that is smaller than the interval you actually want to use?