-1

Need to poll a service based on result of the GET service in angular

I have a reporting service which has the response as follows

{url}/report/72?sortby=label1

response

While the report is being computed

{
    "id": "72",
    "status": "computing"
}

On successful completion success

{
"id": "72",
"status": "completed",
"data": {
    "columns": ["uuid", "label1", "label3"],
    "rows": [
        ["xyghj", 927, 955]
    ]
}
}

I need to retry the call till I get "completed" status in the response and then defer the result

David L
  • 32,885
  • 8
  • 62
  • 93
  • Have you tried anything yet? You really just need to wrap the requests in a `$q` deferred object that resolves when you get the completed status. What should the polling interval be? – Phil May 11 '17 at 01:16
  • polling interval can be 1 second – Rahul Bhandari May 11 '17 at 17:04

1 Answers1

0

Did something similar years ago. This is not tested, and my angularjs might be weak, but the idea is this:

function poll() {
    service.checkPoll().then(function(payload) {
        if (payload.status !== "completed") {

            if (iShouldTryAgain) {
                $timeout(function() {
                    poll();
                }, 1000);
            }
        }
        else {
            getResults(payload.data);
        }
    }, function(error){

    });
}

checkPoll() returns a promise, and is the result of your API call.

payload.status is what tells you if your report is ready or not.

This function will poll every second, but you should max out with iShouldTryAgain to make sure you don't sit there forever. Maybe some max value.

Ben
  • 1,169
  • 3
  • 17
  • 27
  • How does any data get out of this? There's nothing here to signal to the caller that the operation is complete – Phil May 11 '17 at 01:33
  • Added the else to handle the resulting report data. It will poll as long as status is not completed, or some other "iShouldTryAgain" check against a counter or timer. – Ben May 11 '17 at 02:17