1

我有这个功能。我想要这些行:

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));
}
4

1 回答 1

0

在最后一个 else 语句中,您需要返回 observable。所以而不是

else {
        Observable.throw(res);
    }

你需要做:

else {
        return Observable.throw(res);
    }
于 2017-07-18T10:04:34.320 回答