I'm using node-request for sending the requests to server to get some report. The thing is server needs some time to generate the report, so it responses with the report state. I'm checking the report state with setInterval()
function, and use clearInterval()
when server sends ready
response. But with this approach, even after I use clearInterval
, responses of earlier requests keep coming, and the response handler runs again and again. This does not cause a lot of harm, but still I believe it can be done better.
Here is my code:
checkForReportReady = setInterval =>
@request URL, options, (err, res, body) =>
console.log err if err
body = JSON.parse body
if body['status'] is 'ready'
clearInterval checkForReportReady
@processReport body
, 1000
What I need: make a request, wait for response, check the status, if status is not ready
- make another request after some timeout, repeat until the status code in response is ready
. If the status is ready - exit the loop (or clear the interval) and run @processReport
.
I tried to make promisified request, and put it into setInterval
, but the result was the same.
P.S. I do not control the server, so I can't change the way it responds or deals with the report.