Suppose I want to send an asynchronous AJAX request to the server, and when it responds send another request and repeat forever:
function sendXHR(url, callback) {
// Send XMLHttpRequest to server and call callback when response is received
}
function infinite() {
sendXHR('url/path', infinite);
}
infinite();
I assume here we would run out of stack space pretty quickly, so how can I do this (without blocking)?
The pattern of passing callbacks around rather than using return is particularly popular with node.js. How do people create infinite loops? I don't believe most JS engines do any kind of tail call optimisation.
infinitecall.callback();insendXHR, and it reportsMaximum call stack size exceededafter around 9500 iterations. Firefox also saystoo much recursion.sendXHRfunction is using synchronous xhr then? Change this to asynchronous and execute the callback on the next tick.