Open History to see the last commit
1<script lang="ts">
2 import { sleep } from '@tanstack/query-test-utils'
3 import { createMutation, setQueryClientContext } from '../../src/index.js'
4 import type { QueryClient } from '@tanstack/query-core'
5β
6 type Props = {
7 queryClient: QueryClient
8 strategy: 'all' | 'allSettled'
9 failOn?: string
10 }
11β
12 let { queryClient, strategy, failOn }: Props = $props()
13β
14 let result = $state('idle')
15β
16 setQueryClientContext(queryClient)
17β
18 const mutation = createMutation(() => ({
19 mutationFn: (file: string) =>
20 sleep(10).then(() => {
21 if (file === failOn) {
22 throw new Error('upload failed')
23 }
24 return `uploaded: ${file}`
25 }),
26 retry: false,
27 }))
28β
29 async function uploadAll() {
30 if (strategy === 'all') {
31 try {
32 const results = await Promise.all([
33 mutation.mutateAsync('file1'),
34 mutation.mutateAsync('file2'),
35 mutation.mutateAsync('file3'),
36 ])
37 result = results.join(', ')
38 } catch (error) {
39 result = `error: ${(error as Error).message}`
40 }
41 } else {
42 const results = await Promise.allSettled([
43 mutation.mutateAsync('file1'),
44 mutation.mutateAsync('file2'),
45 mutation.mutateAsync('file3'),
46 ])
47 result = results
48 .map((r) =>
49 r.status === 'fulfilled' ? r.value : `error: ${r.reason.message}`,
50 )
51 .join(', ')
52 }
53 }
54</script>
55β
56<button onclick={uploadAll}>upload all</button>
57β
58<div>result: {result}</div>