你在打破链条:
const result = next.handle(newRequest).toPromise(); result.catch(async (err) => { ... }); return result;
result.catch 返回一个新的Promise,你的处理程序不会等待你调用的所有动作 catch 。
result.catch
catch
所以你可以写如下:
const result = next.handle(newRequest).toPromise(); return result.catch(async (err) => { ... });
你可能想做的也是 的 当一个正在进行时,不要多次调用refreshToken 强>
cachedRequest: Promise<any>; // define prop in your class ... if (!this.cachedRequest) { this.cachedRequest = this.authService.refreshToken(); } const newToken = await this.cachedRequest; this.cachedRequest = null;
这是一个 的 简单的演示 强> 所以你可以测试它。 ( 我在那里处理404,但没关系 )