You can't loop like this in Javascript. Javascript is an event-driven system. When you are looping like this, no other events can ever get processed. As such, not even your first Ajax call will get processed. In fact, you will probably just drive the JS engine into the ground by running out of some resource as the client tries to make millions of ajax calls all at once.
Instead, you can use the ajax result asynchronously and then decide from the result whether to call it again:
function poll() {
$.ajax(...).then(function(data) {
if (!data.xxxx) {
// call it again after some short delay
setTimeout(poll, 1000);
} else {
// got the result we wanted, process it
}
})
}
// start the polling
poll();
If you want the whole thing to return a promise, you can do this:
function delay(t) {
return new $.Deferred(function(def) {
setTimeout(def.resolve, t);
}).promise();
}
function poll() {
return $.ajax(...).then(function(data) {
if (!data.xxxx) {
// call it again after some short delay
return delay(1000).then(poll);
} else {
// got the result we wanted, process it
return someValue;
}
})
}
// start the polling
poll().then(function(result) {
// process result here
});
P.S. It is pretty much never the right thing to just constantly poll some server as fast as you possibly can. This might work fine if you only have a couple of users, but as soon as you have lots of users, all that will do is overwhelm your server with empty polling requests where most of the time, there's nothing to return to the client. This is nightmare for handling load. It's even better to find an architecture that is not polling, but if you are going to poll, then at least use some sort of timer to poll at some future time rather than as fast as possible.
P.P.S. If you're really just waiting for some value to change on the server, then perhaps you should use a webSocket connection. With that, the client establishes the connection to the server and the connection persists and then at any point in the future, the server can simply send a message to the client. The client doesn't have to poll at all. This can be massively more efficient on your server infrastructure and can deliver more timely results.