Cancel a Request
Cancelling an HTTP request is a common requirement. For example, you could have a queue of requests where a new request supersedes a pending request and that pending request needs to be cancelled.
To cancel a request we call the unsubscribe function of its subscription.
1
@Component({ /* ... */ })
2
export class AppComponent {
3
/* ... */
4
5
search() {
6
const request = this.searchService.search(this.searchField.value)
7
.subscribe(
8
result => { this.result = result.artists.items; },
9
err => { this.errorMessage = err.message; },
10
() => { console.log('Completed'); }
11
);
12
13
request.unsubscribe();
14
}
15
}
Copied!
Copy link