Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

angular cancel http request

// http-cancel.service.ts
// use this service as DI in component
import {Subject} from 'rxjs';
import {Injectable} from '@angular/core';

@Injectable()
export class HttpCancelService{
  private cancelPendingRequestSubject=new Subject<void>()
  cancelPendingRequest$ = this.cancelPendingRequestSubject.asObservable();

  cancelPendingRequest(){
    this.cancelPendingRequestSubject.next();
  }
}

==============================================================
//http-cancel-interceptor.service.ts
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {Observable, takeUntil} from 'rxjs';
import {Injectable} from '@angular/core';
import {HttpCancelService} from './http-cancel.service';
import {tap} from 'rxjs/operators';

@Injectable()
export class CancelInterceptor implements HttpInterceptor{
  constructor(private cancelService: HttpCancelService) {
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(takeUntil(this.cancelService.cancelPendingRequest$
      .pipe(tap(()=> {throw new Error('Request is canceled')}))));
  }
}
==============================================================
 // app.module.ts
  import {HTTP_INTERCEPTORS} from '@angular/common/http';
  ...
  providers: [
    ...
    {provide: HTTP_INTERCEPTORS, useClass: CancelInterceptor, multi: true}
    ...
  ],
Comment

PREVIOUS NEXT
Code Example
Typescript :: does any event get triggered when checked value changes programatically? 
Typescript :: how to remove second square brackets in an array 
Typescript :: coldfusion arrayLast 
Typescript :: angular validator email 
Typescript :: laravel How to print route lists in Blade 
Typescript :: nest js response timeout 
Typescript :: angular find and remove from string 
Typescript :: typeorm select join column querybuilder 
Typescript :: typescript get objects nested in object 
Typescript :: div resize event typescript 
Typescript :: mailbox exists c# 
Typescript :: conditional statements in linux 
Typescript :: Scripts may close only the windows that were opened by them 
Typescript :: show all value_counts pandas 
Typescript :: run an applescript 
Typescript :: typescript module 
Typescript :: react functional components setstate callback 
Typescript :: how to add command line arguments in vscode 
Typescript :: split in angular 8 
Typescript :: <div 
Typescript :: google sheets k format 
Typescript :: nest js crons intialization 
Typescript :: how test with limited information 
Typescript :: how to compare two entity objects in c# to update 
Typescript :: why are inline scripts not working anymore on HTML 
Typescript :: Could not resolve all artifacts for configuration 
Typescript :: field sets in salesforce 
Typescript :: js convert to typescript online 
Typescript :: inherit with filter typescript 
Typescript :: Modify the program so it also prints the number of A, T, C, and G characters in the sequence in python 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =