I'm trying to create asynchronous task in NestJs. I want that after one request to some controller was made, my async task start begin your long job while loop, and then the node/nest process steel able to answer other requests of other controllers.
But when the async loop begin, all the other endpoints of Nest being freeze until de async task are done.
I already tryed to use setTimeout(), setInterval(), setIntermmediate(), queueMicrotask() and Promise.resolve()
My code:
// Async task:
private checkTime(baseTime: number, startTimeStamp: number){
while(true){
const elapsedTime = Date.now() - startTimeStamp
console.log(`Elapsed time: ${elapsedTime}`)
if(elapsedTime > baseTime){
console.log(`Time excced!`)
break;
}
}
}
And I already try something like that:
queueMicrotask(() => this.checkTime(edge.baseTime, startedTimeStamp))
setTimeout(() => this.checkTime(edge.baseTime, startedTimeStamp), 0)