Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rxjs interval未轮询可观察的服务器API调用_Rxjs_Angular8_Rxjs6 - Fatal编程技术网

Rxjs interval未轮询可观察的服务器API调用

Rxjs interval未轮询可观察的服务器API调用,rxjs,angular8,rxjs6,Rxjs,Angular8,Rxjs6,我正在尝试轮询对后端的API调用。其思想是,服务器将发送一个202错误,直到它完成了一个作业的处理,并且在如此多的请求之后,将返回一个带有一些结果的200。我不想让错误扼杀流。API调用只进行一次 “rxjs”:“~6.4.0”, “@angular/core”:“~8.2.14” 启动代码: onSubmit() { return this.scraperService.postUrl(this.form.value.url) .pipe( swi

我正在尝试轮询对后端的API调用。其思想是,服务器将发送一个202错误,直到它完成了一个作业的处理,并且在如此多的请求之后,将返回一个带有一些结果的200。我不想让错误扼杀流。API调用只进行一次

“rxjs”:“~6.4.0”, “@angular/core”:“~8.2.14”

启动代码:

     onSubmit() {
    return this.scraperService.postUrl(this.form.value.url)
      .pipe(
        switchMap( val => {
          return this.scraperService.pollUntilTaskFinished(val);
        })
      ).subscribe( val => console.log(val))
  }
服务代码:

     postUrl(url: string): Observable<any> {
    return this.http.post('/api/start', {url})
      .pipe(
        map((res: { jobId: string }) => {
          if (res.jobId) {
            return res.jobId;
          }
        }));
  }
  pollUntilTaskFinished(jobId): Observable<any> {
        return interval(2000)
          .pipe(
            switchMap(() => this.http.get(`/api/results/${jobId}`)))
          .pipe(
            catchError(err => this.handleError(err)),
            map(res => console.log(res)));
      }
   handleError(data: HttpErrorResponse) {
    if (data.status === 202) {
      return of('continue');
    }
  }
postrl(url:string):可观察{
返回此.http.post('/api/start',{url})
.烟斗(
映射((res:{jobId:string})=>{
if(res.jobId){
返回res.jobId;
}
}));
}
pollUntilTaskFinished(jobId):可观察{
返回间隔(2000)
.烟斗(
switchMap(()=>this.http.get(`/api/results/${jobId}'))
.烟斗(
catchError(err=>this.handleError(err)),
map(res=>console.log(res));
}
handleError(数据:HttpErrorResponse){
如果(data.status==202){
归还(“继续”);
}
}

我如何确保间隔重复,直到我得到一个包含我需要的JSON的200

如果不想处理该链,则必须先捕获错误,然后再将其传播到主链。这意味着在
switchMap()中捕获它:


您完全正确,错误发生在服务器上,未被捕获。谢谢
this.http.get(`/api/results/${jobId}`))
  .pipe(
    catchError(err => this.handleError(err)),
  )