I am trying to poll an API endpoint (which gives an instant response) until it gives me a successful response and then save the result of the response
If an API has data for a given jobId
it returns a string, and returns 400 status otherwise (I have full control over the API, so the behaviour can be adjusted). I want to poll the API endpoint every three seconds to see if it has an output.
I am a complete beginner with javascript and its derivatives, so below is the function I hacked
function getJobResults(jobId) {
setTimeout(function() {
$.get('/check_for_results/' + jobId, function(data, status) {
return data
})
.fail(function() {
getJobResults(jobId);
});
}, 3000)
}
How do I modify it so that I could do the following?
var expectedStringResult = getJobResults(jobId)
Currently, it does not return a result. Also, how could I rewrite the function to make it more sensible?
Thank you!