Angular2 observable http get conditional repeat

爷,独闯天下 提交于 2019-12-22 08:22:41

问题


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


回答1:


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()


来源:https://stackoverflow.com/questions/44933284/angular2-observable-http-get-conditional-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!