How to cancel HTTP request in Angular 8

0 votes
Can you tell me with the help of cod and an example How to cancel HTTP request in Angular 8?
Feb 26 in Angular by Nidhi
• 10,860 points
39 views

1 answer to this question.

0 votes

In Angular 8, you can cancel an HTTP request using the takeUntil operator with an RxJS Subject.

Method : Using takeUntil

import { HttpClient } from '@angular/common/http';

import { Component, OnDestroy } from '@angular/core';

import { Subject } from 'rxjs';

import { takeUntil } from 'rxjs/operators';


@Component({

  selector: 'app-example',

  templateUrl: './example.component.html',

})

export class ExampleComponent implements OnDestroy {

  private destroy$ = new Subject<void>();


  constructor(private http: HttpClient) {}


  fetchData() {

    this.http.get('https://api.example.com/data')

      .pipe(takeUntil(this.destroy$))

      .subscribe(response => {

        console.log(response);

      });

  }


  cancelRequest() {

    this.destroy$.next(); // Cancels the ongoing request

    this.destroy$.complete();

  }


  ngOnDestroy() {

    this.cancelRequest();

  }

}

answered Feb 26 by Kavya

Related Questions In Angular

0 votes
1 answer
0 votes
2 answers

How to detect a route change in Angular?

Hii Kartik For Angular 7 someone should write like: this.router.events.subscribe((event: Event) ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
29,492 views
0 votes
3 answers

How to load external scripts dynamically in Angular?

Hello kartik, You can use following technique to ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
72,448 views
0 votes
1 answer

How to increase request timeout in IIS?

Hello @kartik, To Increase request time out add ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,840 points
9,456 views
0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,365 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28, 2020 in Java-Script by Niroj
• 82,840 points
19,346 views
0 votes
1 answer

Why it is necessary to refresh CSRF token per form request?

Hello, Generating a new CSRF token for each ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,840 points
4,792 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,395 views
0 votes
1 answer

How to transfer data between two unrelated components in Angular?

Steps to Transfer Data Between Unrelated Components 1. ...READ MORE

answered Dec 12, 2024 in Angular by Navya
170 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP