我可以在rxJS angular2中重试后调用重试

时间:2016-11-02 04:05:04

标签: angular typescript rxjs

我想在重试后调用重试当没有工作时,如何在重试后调用相同的observable。我的代码没有达到重试次数。

getStatuses(statuses : string[]): Observable<IStatus[]> {
    let body = JSON.stringify({ "Statuses": statuses });
    let headers = new Headers({ 'Content-Type': 'application/json' });
    headers.append('Authorization', 'Bearer' + localStorage.getItem("access_token");
    headers.append('Access-Control-Allow-Origin', '*');
    let options = new RequestOptions({ headers: headers });
    return this.http.post(this._baseUrl + '/statuses/statusesList', body, options)
               .retryWhen(error => this.refreshToken(error)).retry(3)
                .map((res: Response) => {
                    this.inspections = res.json();
                    return this.inspections;
                });
}

这是ny refreshToken()函数

refreshToken(error$:any): Observable<any> {
    return error$.flatMap((err:Error) => {
        console.log("refreshing token");
        const body: string = 'grant_type=refresh_token&refresh_token=' + 
                                localStorage.getItem("refresh_token");
        const headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');

        const options = new RequestOptions({ headers: headers });

        // Make the refresh call as normal
        return this.http.post('https://loginqa.test.com/as/token.oauth2', body, options)
    }).map((res: Response) => {
             return res.json();

        })

        .do((resp:Response) => {
           var resultsToken=resp.json();
           alert("testing refresh_token");
            localStorage.removeItem('access_token');
            localStorage.removeItem('refresh_token');
            localStorage.setItem("access_token", resultsToken.access_token);
            localStorage.setItem("refresh_token", resultsToken.refresh_token);

}

1 个答案:

答案 0 :(得分:2)

问题是你的refreshToken函数实际上没有处理或发出错误。这是为了达到retry所需要做的事情。

subscribe块中,您没有订阅throw运算符,因此错误永远不会发生在任何地方。

我会重构refreshToken,以便在失败时实际发出错误。

// Pass in the error stream so you are actually processing when there is an
// exception
refreshToken(error$): Observable<any> {

  return error$.flatMap(err => {
    console.log("refreshing token");
    const refreshToken = localStorage.getItem('refreshToken');
    const body: string = `grant_type=refresh_token&refresh_token=${refreshToken}`;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    const options = new RequestOptions({ headers: headers });

    // Make the refresh call as normal
    return this.http.post('https://fedqa.test.com/as/token.oauth2', body, options);
  })
  .map(res => res.json())
  .do(resp => {
    // If there is no error, this will emit a next value
    // and which will trigger the retry
    // An error triggers, then this will not run and the retryWhen
    // will skip ahead to the retry operator.
    localStorage.removeItem('access_token');
    localStorage.removeItem('refresh_token');
    localStorage.setItem("access_token", resp.access_token);
    localStorage.setItem("refresh_token", resp.refresh_token);
    localStorage.setItem("expires_in", resp.expires_in);
  });


}

然后使用它:

.retryWhen(error$ => this.refreshToken(error$)).retry(3);