由于我是棱角分明的新手,我想知道应该达到我想要的最佳做法。
我想请求一个API,直到我得到我想要的结果,并且请求之间有间隔。
我使用两个嵌套服务(GiftsService和ApiService):
gifts.service.ts:
import { Injectable } from '@angular/core';
import { URLSearchParams } from '@angular/http';
import { environment } from '../../../environments/environment';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { ApiService } from './api.service';
import { Gift } from '../models';
@Injectable()
export class GiftsService {
constructor (
private apiService: ApiService
) {}
get(id): Observable<Gift> {
return this.apiService.get('/gifts/' + id)
.map(data => data);
}
...
api.service.ts
import { Injectable } from '@angular/core';
import { environment } from '../../../environments/environment';
import { Headers, Http, Response, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { JwtService } from './jwt.service';
@Injectable()
export class ApiService {
constructor(
private http: Http,
private jwtService: JwtService
) {}
get(path: string, params: URLSearchParams = new URLSearchParams()): Observable<any> {
return this.http.get(path, { headers: this.setHeaders(), search: params })
.catch(this.formatErrors)
.map((res:Response) => res.json());
}
...
我在这样的组件中调用GiftsService.get:
this.giftsService.get(this.gift.id)
.subscribe(data => {
// if data.status != "ok" retry
})
更新
我这是怎么做的
this.giftsService.get(this.gift.id)
.switchMap(data => data.status !== 'success' ? Observable.throw(new Error('status not successful')) : Observable.of(data))
.retryWhen(attempts => {
return Observable
.range(1,10)
.zip(attempts, function(i) {
return(i);
})
.flatMap(function(i) {
return Observable.timer(1000);
})
})
.subscribe((
data => {
this.gift = data
})
);
答案 0 :(得分:1)
您可以这样做:
this.giftsService.get(this.gift.id)
.switchMap(data => data.status !== 'OK' ? Observable.throw() : Observable.of(data))
.retryWhen(errors => { /* setup retry logic here */ })
.subscribe(data => {})
答案 1 :(得分:0)
您可以将retryWhen
运算符与range
和timer
结合使用。
get(path: string, params = new URLSearchParams()) {
return this.http.get(path, {
headers: this.setHeaders(), search: params
})
.map(response => {
response = response.json();
if(response.status !== 'ok') {
throw new Error('status not okay');
}
return response;
})
.retryWhen(errors => {
return Observable
.range(1, 3 /* maximum retry attempts */)
.zip(errors)
.flatMap([attempt, error] => {
if(attempt >= 3) {
return Observable.throw(error);
}
return Observable.timer(attempt*500 /* the retry timeout which increases after each attempt */);
})
})
.catch(this.formatErrors)
}