angular how to cancel an http request Edit

Murugan Andezuthu Dharmaratnam | 21 February 2021 | 92

Canceling of the HTTP request is important when you write code like to check if the username already exists during sign up. In this case, when the user starts typing the name in an input element several HTTP requests are sent to the server to check if the user name already exists. So it's required to cancel all the previous requests and has only the last request. The below code shows how this can be done. In my case in the below code, I am checking if SKU already exists on the server.

solution is to use the request.unsubscribe();


  public req: Subscription;
  public SKUExistVariant() 
    if(this.req != undefined)
    this.req = this.itemService.get('api/item/SKUExist?SKU=' this.oldSKU).subscribe(
      data => {
        // do something