Skip to content

Instantly share code, notes, and snippets.

@kerimdzhanov
Created September 4, 2020 20:32
Show Gist options
  • Save kerimdzhanov/c6b077deba12d0c3da3cb9f6f2739cc7 to your computer and use it in GitHub Desktop.
Save kerimdzhanov/c6b077deba12d0c3da3cb9f6f2739cc7 to your computer and use it in GitHub Desktop.
export class CancellableRequest {
private controller = new AbortController();
public fetch(input: RequestInfo, init: RequestInit = {}): Promise<Response> {
init.signal = this.controller.signal;
return fetch(input, init)
.then(res => res.json());
}
public isCancelled(): boolean {
return this.controller.signal.aborted;
}
public cancel(): void {
this.controller.abort();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment