kontent-ai / delivery-sdk-js

Kontent Delivery SDK for Javascript
https://kontent.ai
MIT License
50 stars 34 forks source link

cancelToken not canceling the request? #378

Closed sahibalejandro closed 10 months ago

sahibalejandro commented 11 months ago

Brief bug description

Passing a cancel token using .queryConfig({ cancelToken: <token> }) seems to be not working, the request is not canceled when calling the cancel() method for the <token>'s source.

What went wrong? Request is not canceled when it should be.

Repro steps

Create a cancel token source using Axios source = axios.CancelToken.source();

Assign the token to a client request client.item<SomeKontentItem>('some_item').queryConfig({ cancelToken: source.token }).toPromise()

Call the cancel() method source.cancel()

Expected behavior

The HTTP request should be canceled at the time cancel() function is called

Test environment

Additional context

Doing the exact same steps but using axios.get(..., { cancelToken: source.token }) instead of delivery client works as expected.

Enngage commented 10 months ago

Hi @sahibalejandro,

That is likely because you can't pass axios cancel token directly - we have an abstraction over it because some people might be using custom HttpService and skipaxios all together.

If you are not using custom HttpService, have you tried generating token like this?

import { createCancelToken } from '@kontent-ai/core-sdk';

const token = createCancelToken();