Home > OS >  Javascript sleep code running, but seems to not cause any delays
Javascript sleep code running, but seems to not cause any delays

Time:10-08

This an Angular app, and this specific code is inside a webworker in Typescript. I'm still new to webworkers, but both the sleep and the loop execute inside the same thread.

The intent is to poll a service and exit the loop when a the process is completed. My problem is the sleep call below is not sleeping. I need it to delay for at least 9 seconds, and ideally I'd like it to be configurable. But it runs as though the sleep didn't run.

I have two questions:

  1. Why is the sleep not working?
  2. This is an Angular app served by a NodeJS container on cirrus. When the Angular app is requested and served by this NodeJS server, I'd like to pass a secret defined at the NodeJS server along with the Angular app. This secret would be the polling delay. Would a cookie value be returned in the NodeJS Angular App response? Not sure what the best response would be.

Code below:


function sleep(ms: number) {
    return new Promise((resolve) => {
        log('DEBUG','Sleeping for '   ms   ' ms');
        setTimeout(resolve, ms);
    });
}

while (jobIsStillRunning(jobExecutionResult)) {
    postMessageWithLog(jobExecutionResult);
    log('DEBUG','sendFilePolling() jobExecutionResult='   JSON.stringify(jobExecutionResult));
    sleep(30000);  // Sleep function runs but doesn't do anything.
    jobExecutionResult = await getAsyncFilesResult(jobExecutionResult);
}

CodePudding user response:

You need to await on sleep function

CodePudding user response:

I think you need to know macro and micro tasks.

Microtasks come solely from our code. They are usually created by promises: an execution of .then/catch/finally handler becomes a microtask

If a microtask recursively queues other microtasks, it might take a long time until the next macrotask is processed. This means, you could end up with a blocked UI, or some finished I/O idling in your application.

example:

macrotasks: setTimeout, setInterval, setImmediate, I/O, UI rendering, etc/

microtasks: process.nextTick, Promises, etc.

code:

console.log('1')

setTimeout(()=>{
  console.log('2')
},0)

Promise.resolve().then(()=>{
  console.log('3')
})

console.log('4')

output:

1
4
3
2

wait what?!

explain:

  • console.log('1') it's a normal code and immediately run.
  • setTimeout it's a macrotask then it will add to macro queue
  • Promise.resolve it's a microtask then it will add to micro queue
  • console.log('4') it a normal code and immediately run.

now when normal codes are end and now microtaskqueue and macrotaskqueue need to be dequeue.

  • inside microtaskqueue we have one task and it's Promise.resolve then immediately run .then function

now microtaskqueue is clear too, inside macrotaskqueue we have one task and it's setTimeout then immediately run timeoutCallback function

if you want to use sleep function without await you need to handle this. but you can add await before your sleep function and wait on it

  • Related