Skip to main content
Version: 5.x.x

Request Cancellation


Canceling requests is needed for various reasons. There are cases when we make the request and we want to abort it manually or the case when dispatcher is automatically aborting it based on our setup.


Dispatcher cancelable mode

One of the dispatcher's modes is cancelable one. It is triggered when previous request is in progress and we trigger the new one and cancel previous at the same time to prevent race-conditioning of the responses. To enable this mode, we need to set the cancelable: true field to request options.

const getUsers = client.createRequest<UsersList>()({  endpoint: "/users", cancelable: true });

// Make a request to server
getUsers.send()
...
// Cancel the previous request and trigger new one
getUsers.send()

Manual cancellation by abortKey

Each request has its own abortKey, which may be the same across request groups. Thanks to it, we have the ability to abort ongoing requests.

// With useFetch
const { abort } = useFetch();

abort();

// With useSubmit
const { abort, submit } = useSubmit();

submit();
...
abort();

Dispatcher built-in queue/request stopping

When we stop sending all requests from further on in the queue, we do not want them to be deleted, but canceled, so that we can resume them at any time we choose.

// Make a request to server
postFile.send()

...

// Stop the request from being send for later
// Do not delete it from storage to trigger it later
client.fetchDispatcher.stop(postFile.queueKey)

...

// Start sending again
client.fetchDispatcher.start(postFile.queueKey)