Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 observable http get conditional repeat

I'm using angular2 observable pattern to make http requests. I'm trying to conditional repeat the http get: I want to execute the http get until a condition is met:

http.get('url')
.map(res => {
     // if the condition is met I should repeat the http get request
})
.subscribe()

Is there a way to conditional repeat the http get request?

Thanks, Marco

like image 470
Marco Antelmi Avatar asked Jul 05 '17 18:07

Marco Antelmi


1 Answers

You can use expand operator. Here's an example:

let request$ = http.get('url');

request$.expand(value => {
  return value !== 0 ? request$ : Rx.Observable.empty()
})
.map(res => {
  //Do mapping here
})
.subscribe()
like image 123
AhmedRiyad Avatar answered Oct 19 '22 12:10

AhmedRiyad