When operation timeout period starts

It seems that the client starts timeout period not right before performing an operation. Is it expected and documented somewhere?

Repo to reproduce:

Hi @bvv

This is expected behavior due to the nature of Node.js.

In your example, you are polling the event loop. In node.js, this prevents other function from executing until the polling operation ends or blocks. However, since these functions are queued during the polling operation and are waiting for execution, the timeout timer begins before the function begins executing.

To fix this, simply block after your polling operation, or block during your polling operation to release the event loop.

blockEventLoopForMs(totalTimeout + 300);

await new Promise(r => setTimeout(r, 1));

You can also use two setImmediatePromise() calls rather than a 1 ms setTimeout.

Polling on the event loop can cause problems in Nodejs, so I would encourage you to take advantage of the asynchronous non-blocking tools and methods to facilitate execution order rather the relying on a polling operation to block execution because issues such as these can arise.

If this solution will not work for you, I would be happy to troubleshoot further for you if you explain why the expected behavior is necessary for your project.


Hi dpelini, thanks for you replay. What do you mean by these methods?