我有这个功能。我想要这些行:
this.setHeaders(url);
return this.request(url, options);
时执行this.refreshTokenService.wait === false
。
private catchErrors(url: string | Request, options?: RequestOptionsArgs) {
return (res: Response) => {
if (res.status === 401 || res.status === 403) {
if (this.refreshTokenService.wait === false) {
console.log('FIRST');
this.refreshTokenService.wait = true;
return this.refreshTokenService.refreshToken(localStorage.getItem('JWToken'))
.flatMap((result: any) => {
// if got new access token - retry request
localStorage.setItem('JWToken', JSON.parse(result._body).token);
this.setHeaders(url);
this.refreshTokenService.wait = false;
return this.request(url, options);
})
} else {
// TODO here
this.setHeaders(url);
return this.request(url, options);
}
} else {
Observable.throw(res);
}
};
}
但是该函数只想返回 observable 或承诺顺利运行。
谢谢
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
if (typeof url === 'string') {
if (!options) {
options = {headers: new Headers()};
}
this.setHeaders(options);
} else {
this.setHeaders(url);
}
return super.request(url, options).catch(this.catchErrors(url, options));
}