What can I replace the tap method with in my Angular app?

Issue

I have the following code as part of an http-interceptor:

handler(next, request) {
    return next.handler(request)
        .pipe(
            tap(
                (event) => {
                    if (event instanceof HttpResponse) {
                        this.spinnerService.requestEnded();
                    }
                },
                (error: HttpErrorResponse) => {
                    this.spinnerService.resetSpinner();
                    throw error;
                }
            ),
        );
}

It uses tap (line 4) which has been deprecated since Angular 8. How can I replace tap? The searched I’ve done have not helped me find an answer. Thanks.

Solution

Use { next, error } object

   tap({
       next: (event) => {
           if (event instanceof HttpResponse) {
               this.spinnerService.requestEnded();
           }
       },
       error: (error: HttpErrorResponse) => {
           this.spinnerService.resetSpinner();
           throw error;
       }
   })

Answered By – Евгений Тишкевич

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.