2021年3月17日星期三

Call async function into catch from interceptor Angular

I need to call an async method to wait after call other function. I'm not very expert in terms of rxjs and also I've never worked with async/await calls.

I have this logic into the interceptor

public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {        if (req.headers.get('a-header')) {        const headers = req.headers.delete('a-header');          return next.handle(req.clone({ headers, withCredentials: true }));      }        return next.handle(req.clone({ withCredentials: true })).catch((err: Error) => {        if (!(err instanceof HttpErrorResponse) || (err as HttpErrorResponse).status !== 401) {          return throwError(err);        }        this.error.notify401Error(err);        //NEED TO AWAIT THE NEXT RESPONSE        this.callXApi(next);        //AND THEN CONTINUE WITH THIS CALL        this.redirectSignedOut();        return empty();      });    }  

Note that I need to call CallXApi, wait until finished, and then continue with redirectSignedOut function.

  private callXApi(): void {      const { uri } = this.config;        this.httpClient        .delete(uri, { withCredentials: true })        .subscribe(() => { console.log('completed.') }, (err) => throwError(err));    }  

How can I achieve that?

https://stackoverflow.com/questions/66683021/call-async-function-into-catch-from-interceptor-angular March 18, 2021 at 08:08AM

没有评论:

发表评论