I have a function in javascript that sends a GET request using AJAX. The request either returns success
or fail
or in process
if the job isn't complete yet. I would like to have a function to send this GET request every few seconds until the result is either success
or fail
and return that result. This is what I have so far but I can't get the return value since setTimeout is calling the function again. Is there a way to put this function as a promise and return the result when it's done?
function get_r(name) {
$.ajax({
url: "/get/" + name
}).done(function( result ) {
if (JSON.parse(result) === 'success') {
return "Finished"
}
else if (JSON.parse(result) === 'fail') {
return "Failed"
}
else {
setTimeout(function() { get_r(name); }, 1000);
}
});