File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Cancel a HTTP request
2
+
3
+ > The ` abort() ` method of the AbortController interface aborts a DOM request (e.g. a Fetch request)
4
+ >
5
+ > -- [ AbortController interface] ( https://developer.mozilla.org/en-US/docs/Web/API/AbortController )
6
+
7
+ References -
8
+ * [ AbortController interface] ( https://developer.mozilla.org/en-US/docs/Web/API/AbortController )
9
+ * [ abortcontroller npm] ( https://www.npmjs.com/package/abort-controller )
10
+ * [ abortcontroller-polyfill] ( https://www.npmjs.com/package/abortcontroller-polyfill )
11
+ * [ Example of the AbortController implementation] ( https://github.com/node-fetch/node-fetch#request-cancellation-with-abortsignal )
12
+
13
+ #### Following is how canceling a fetch call works:
14
+
15
+ * Create an AbortController instance.
16
+ * That instance has a signal property.
17
+ * Pass the signal as a fetch option for signal.
18
+ * Call the AbortController's abort property to cancel all fetches that use that signal.
19
+
20
+ #### Setting the AbortController.signal as a fetch option while creating the MSGraph SDK Client instance:
21
+
22
+ ``` typescript
23
+ import { Client ,FetchOptions } from " @microsoft/microsoft-graph-client" ;
24
+ import { AbortController } from " abort-controller" ; // <- import when using the abort-controller npm package.
25
+
26
+ const controller = new AbortController ();
27
+
28
+ const timeout = setTimeout (() => {
29
+ controller .abort ();
30
+ }, 150 );
31
+
32
+ const fetchOptions: FetchOptions = {
33
+ signal: controller .signal ;
34
+ }
35
+
36
+ const client = Client .initWithMiddleware ({
37
+ fetchOptions , // Pass the FetchOptions value where the AbortController.signal is set
38
+ authProvider ,
39
+ ...
40
+ });
41
+ ```
You can’t perform that action at this time.
0 commit comments