angular - Angular4: HttpClient - many requests in succession -




i have http request list of items feed. feed api requires page param, limited 20. first 60 instead, need make 3 requests

requestoptions.params.set('page', 1); this.http.get(environment.api+ '.feed.json', requestoptions) requestoptions.params.set('page', 2); this.http.get(environment.api+ '.feed.json', requestoptions) requestoptions.params.set('page', 3); this.http.get(environment.api+ '.feed.json', requestoptions) 

what best way deal ordered list? not fond of nesting

your service:

getpage(page) {   /*init requestoptions*/   requestoptions.params.set('page', page);   return this.http      .get(environment.api+ '.feed.json', requestoptions)      .map(/*map object expected one*/)      .catch(/*catch http exception*/); } 

somewhere call service

let allpages = [];  observable.forkjoin(   this.service.getpage(1);   this.service.getpage(2);   this.service.getpage(3); ).subscribe((resparray) =>{    allpages = allpages.concat(resparray[0]);    allpages = allpages.concat(resparray[1]);    allpages = allpages.concat(resparray[2]); }); 

object.forkjoin wait until requests completed , can build single list results;





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -