Skip to content

Is there a way to implement a "cancel token" feature? #296

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

Closed
plamber opened this issue Jun 27, 2020 · 5 comments
Closed

Is there a way to implement a "cancel token" feature? #296

plamber opened this issue Jun 27, 2020 · 5 comments

Comments

@plamber
Copy link
Contributor

plamber commented Jun 27, 2020

In some scenarios you might have the necessity to implement a cancel token pattern to avoid executing HTTP calls if they are no more valid.
An example for such a scenario might be a people picker that executes HTTP call requests but stops once the state for the typed in values has changed.
You can find here an example how such a cancel token might work in react and Axios: https://stackoverflow.com/questions/42896831/cancel-axios-get-request-when-typing-reactjs
Is there a way to have a similar pattern implemented with the Graph SDK?
AB#5998

@MIchaelMainer
Copy link
Contributor

Yes, this would be helpful for us to support in the client. Thank you for bringing this up.

@ddyett ddyett added enhancement ADO to GitHub automation label Issue caused by core project dependency modules or library labels Sep 14, 2020
@ddyett ddyett removed the ADO to GitHub automation label Issue caused by core project dependency modules or library label Feb 9, 2021
@nikithauc nikithauc self-assigned this Mar 29, 2021
@nikithauc
Copy link
Contributor

@plamber. It is possible can a request currently when using the MS Graph JS SDK.

References to cancel a fetch request -
You can use the interface FetchOptions.
https://developer.mozilla.org/en-US/docs/Web/API/AbortController
https://github.com/node-fetch/node-fetch#request-cancellation-with-abortsignal

Example -

const AbortController = require('abort-controller');

const controller = new AbortController();
const timeout = setTimeout(() => {
	controller.abort();
}, 140);

const fetchOptions = {
	signal: controller.signal // set the fetchOptions Signal
}


const client = Client.initWithMiddleware({
		debugLogging: true,
		authProvider,
		fetchOptions // Set the fetch options here. 
		
});

@mitani
Copy link

mitani commented Jun 18, 2021

@nikithaqprevent won't this only work if you use 1 client per request. I would have assumed the timeout would need to wrap around an abort controller for each request, and each request would need to have it's own fetch options so that it resets the timer per request.

@nikithauc
Copy link
Contributor

@mitani

You can also use the options() api in the GraphRequest ,that is, client.api().options().get() instead of setting the options while initializing the client.

Is that what you are looking for?

@mitani
Copy link

mitani commented Jun 18, 2021

Yes, thank you @nikithauc it would be great if the example also reflected that use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants