I am in the early phases of an Angular 4 project trying to create a client for my http services. These services can return one of two things:
- The actual desired json response from the service with an http status of 200, or
- A guid which represents a job token with which I can poll for status of the long-running job, and an http status of 202.
When the service returns a 200 status, I want to just return its json content directly, so that's easy enough.
When the result status is a 202, I then want to poll a separate url with that token and receive a json object with status info. When that status info indicates completion, I want to then make a request to a final url that will return the output.
I'm not sure how to handle the conditional logic that I need within the request. I suspect I could use repeatWhen to monitor the result of the polling request, but I'm having trouble figuring out how to use it.
http.request('/some/longrunning/job')
.flatMap((resp: Response) => {
if (resp.status !== 202)
return Observable.of(resp);
var job_token = resp.json().token;
return http.get('/jobstatus/' + job_token)
// result from this endpoint is a json object that looks something
// like { status: 'running' } or { status: 'complete' }
.repeatWhen(function (polling_resp: Observable<Response>) {
// How do I use polling_resp here to look at my http response?
// Do I need to explicitly subscribe to it? And if so,
// how then do I inspect the response and return the
// appropriate value to indicate whether or not to
// resubscribe via the repeatWhen?
});
});
Can someone give me some hints on how to use logic in the repeatWhen? I haven't seen any examples that use content from the observable itself to decide whether to resubscribe.