Open History to see the last commit
1---
2id: query-cancellation
3title: Query Cancellation
4ref: docs/framework/react/guides/query-cancellation.md
5replace:
6 {
7 '@tanstack/react-query': '@tanstack/svelte-query',
8 'useQuery[(]': 'createQuery(() => ',
9 }
10---
11β
12[//]: # 'Example'
13β
14```ts
15const todosQuery = createQuery(() => ({
16 queryKey: ['todos'],
17 queryFn: async ({ signal }) => {
18 const todosResponse = await fetch('/todos', {
19 // Pass the signal to one fetch
20 signal,
21 })
22 const todos = await todosResponse.json()
23β
24 const todoDetails = todos.map(async ({ details }) => {
25 const response = await fetch(details, {
26 // Or pass it to several
27 signal,
28 })
29 return response.json()
30 })
31β
32 return Promise.all(todoDetails)
33 },
34}))
35```
36β
37[//]: # 'Example'
38[//]: # 'Example2'
39β
40```ts
41import axios from 'axios'
42β
43const todosQuery = createQuery(() => ({
44 queryKey: ['todos'],
45 queryFn: ({ signal }) =>
46 axios.get('/todos', {
47 // Pass the signal to `axios`
48 signal,
49 }),
50}))
51```
52β
53[//]: # 'Example2'
54[//]: # 'Example3'
55β
56```ts
57import axios from 'axios'
58β
59const todosQuery = createQuery(() => ({
60 queryKey: ['todos'],
61 queryFn: ({ signal }) => {
62 // Create a new CancelToken source for this request
63 const CancelToken = axios.CancelToken
64 const source = CancelToken.source()
65β
66 const promise = axios.get('/todos', {
67 // Pass the source token to your request
68 cancelToken: source.token,
69 })
70β
71 // Cancel the request if TanStack Query signals to abort
72 signal?.addEventListener('abort', () => {
73 source.cancel('Query was cancelled by TanStack Query')
74 })
75β
76 return promise
77 },
78}))
79```
80β
81[//]: # 'Example3'
82[//]: # 'Example4'
83β
84```ts
85const todosQuery = createQuery(() => ({
86 queryKey: ['todos'],
87 queryFn: ({ signal }) => {
88 return new Promise((resolve, reject) => {
89 var oReq = new XMLHttpRequest()
90 oReq.addEventListener('load', () => {
91 resolve(JSON.parse(oReq.responseText))
92 })
93 signal?.addEventListener('abort', () => {
94 oReq.abort()
95 reject()
96 })
97 oReq.open('GET', '/todos')
98 oReq.send()
99 })
100 },
101}))
102```
103β
104[//]: # 'Example4'
105[//]: # 'Example5'
106β
107```ts
108const client = new GraphQLClient(endpoint)
109β
110const todosQuery = createQuery(() => ({
111 queryKey: ['todos'],
112 queryFn: ({ signal }) => {
113 return client.request({ document: query, signal })
114 },
115}))
116```
117β
118[//]: # 'Example5'
119[//]: # 'Example6'
120β
121```ts
122const todosQuery = createQuery(() => ({
123 queryKey: ['todos'],
124 queryFn: ({ signal }) => {
125 const client = new GraphQLClient(endpoint, {
126 signal,
127 })
128 return client.request(query, variables)
129 },
130}))
131```
132β
133[//]: # 'Example6'
134[//]: # 'Example7'
135β
136```svelte
137<script lang="ts">
138 const todosQuery = createQuery(() => ({
139 queryKey: ['todos'],
140 queryFn: async ({ signal }) => {
141 const resp = await fetch('/todos', { signal })
142 return resp.json()
143 },
144 }))
145β
146 const queryClient = useQueryClient()
147</script>
148β
149<button
150 onclick={() => {
151 queryClient.cancelQueries({ queryKey: ['todos'] })
152 }}
153>
154 Cancel
155</button>
156```
157β
158[//]: # 'Example7'
159[//]: # 'Limitations'
160[//]: # 'Limitations'