Skip to content

Instantly share code, notes, and snippets.

@av1v3k
Created February 12, 2026 11:39
Show Gist options
  • Select an option

  • Save av1v3k/556a97e9b553b29872458a1c8cb9a56f to your computer and use it in GitHub Desktop.

Select an option

Save av1v3k/556a97e9b553b29872458a1c8cb9a56f to your computer and use it in GitHub Desktop.
angular | scenario - clicking any button must cancel any existing API request.
//after click of any buttons in the web application, I need to cancel any existing API request and must trigger the new API request associated with the Button. How do I do it in angular ?
import { Subject, switchMap } from 'rxjs';
// Inside your component
private clickTrigger = new Subject<void>();
// Set up the stream in ngOnInit or the constructor
this.clickTrigger.pipe(
switchMap(() => this.myService.getData())
).subscribe(data => {
this.result = data;
});
<button (click)="onButtonClick()">Fetch Data</button>
onButtonClick() {
this.clickTrigger.next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment