Reputation: 715
I am creating a NodeJS/Express application and am looking to spawn a child-process and get a result to return to the user. If this process takes too long, I want to send the user a timeout message. Here's what I've tried:
app.get("/run-process", function(req, res) {
setTimeout(function(){
return res.send("Timeout");
}, 5000)
// This is to imitate a spawn process that takes too long
while (true) {
console.log("Infinite Loop!")
}
...
Do some other stuff...send response to user.
})
Ideally, after 5 seconds, if no response is sent, the timeout function would run and send a timeout message to the user, AND all other code in this $.get function would stop. However, this is not working. Instead, the loop continues indefinitely and the set timeout function isn't stopping the code.
Your help is appreciated!
Upvotes: 0
Views: 799
Reputation: 375
Once you reach the while(true)
nothing else will execute other than the infinite loop.
The call stack is filled with calls to console.log
and therefore wont ever place the setTimeout
function in it.
Upvotes: 1