Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 1.5 KB

CancellingAHTTPRequest.md

File metadata and controls

41 lines (31 loc) · 1.5 KB

Cancel a HTTP request

The abort() method of the AbortController interface aborts a DOM request (e.g. a Fetch request)

-- AbortController interface

References -

Following is how canceling a fetch call works:

  • Create an AbortController instance.
  • That instance has a signal property.
  • Pass the signal as a fetch option for signal.
  • Call the AbortController's abort property to cancel all fetches that use that signal.

Setting the AbortController.signal as a fetch option while creating the MSGraph SDK Client instance:

import { Client,FetchOptions } from "@microsoft/microsoft-graph-client";
import { AbortController } from "abort-controller"; // <- import when using the abort-controller npm package.

const controller = new AbortController();

const timeout = setTimeout(() => {
	controller.abort();
}, 150);

const fetchOptions: FetchOptions = {
	signal: controller.signal;
}

const client = Client.initWithMiddleware({
  fetchOptions, // Pass the FetchOptions value where the AbortController.signal is set
  authProvider,
  ...
});