Skip to content

Adding cancel HTTP request documentation #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/CancellingAHTTPRequest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Cancel a HTTP request

> The `abort()` method of the AbortController interface aborts a DOM request (e.g. a Fetch request)
>
> -- [AbortController interface](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)

References -
* [AbortController interface](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
* [abortcontroller npm](https://www.npmjs.com/package/abort-controller)
* [abortcontroller-polyfill](https://www.npmjs.com/package/abortcontroller-polyfill)
* [Example of the AbortController implementation](https://github.com/node-fetch/node-fetch#request-cancellation-with-abortsignal)

#### 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:

```typescript
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,
...
});
```