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?
UPDATE
I achieved this by changing the next code
return <any>next.handle(req.clone({ withCredentials: true })).catch(async (err: Error) => {
if (!(err instanceof HttpErrorResponse) || (err as HttpErrorResponse).status !== 401) {
return throwError(err);
}
this.errorEventService.notify401Error(err);
await this.callXApi();
return EMPTY;
});