I'm assuming the answer to this is really simple.
I want to block the nodejs event loop whenever a particular function is running. The function itself might call some async things it waits on, but I don't want anything else to happen while this function is doing its thing.
thanks!
The way to block the event loop in node.js is to make the code synchronous:
while (true) {
// this will block the event loop
}
Recursion is also synchronous
function rec() {
rec();
}
// This one exists with RangeError: Maximum call stack size exceeded
Using any synchronous functions like:
fs.readFileSync()
fs.writeFileSync()
// any sync functions will do
If you intend to create a timeout, that blocks everything just create a for loop to a billion and it will stop for a while.
Your request seems unnatural, maybe it is not node.js that you need. Usually, people in node are trying to make everything asynchronous.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With