Open History to see the last commit
1---
2id: createAsyncQueuer
3title: createAsyncQueuer
4---
5
6```ts
7function createAsyncQueuer<TValue, TSelected>(
8 fn,
9 options?,
10selector?): SvelteAsyncQueuer<TValue, TSelected>;
11```
12
13Defined in: [packages/svelte-pacer/src/async-queuer/createAsyncQueuer.ts:100](https://github.com/TanStack/pacer/blob/main/packages/svelte-pacer/src/async-queuer/createAsyncQueuer.ts#L100)
14
15Creates and retains the AsyncQueuer for its Svelte owner.
16
17Retains items until they are processed. Use addItem to enqueue work and start, stop, execute, clear, or flush to control processing. Selected state exposes pending items, capacity, and completed work.
18
19The callback may return a Promise. Core result, error, retry, and abort behavior is preserved.
20Use onSuccess, onError, and onSettled for execution outcomes.
21
22## State and subscriptions
23
24Pass a selector to track only the state consumed by the owner. The default selection is {},
25so utility state changes do not update the owner unless it opts in. Selection uses shallow
26comparison. The raw store remains available for additional subscriptions.
27Read selected state through utility.state. Use utility.Subscribe with a children snippet
28to select state in a child without updating the owner.
29
30Available state fields:
31
32- `activeItems`: Items currently being processed by the queuer
33- `addItemCount`: Number of times addItem has been called (for reduction calculations)
34- `errorCount`: Number of task executions that have resulted in errors
35- `executionCount`: Number of times execute has been called
36- `expirationCount`: Number of items that have been removed from the queue due to expiration
37- `isEmpty`: Whether the queuer has no items to process (items array is empty)
38- `isExecuting`: Whether the queuer is currently executing
39- `isFull`: Whether the queuer has reached its maximum capacity
40- `isIdle`: Whether the queuer is not currently processing any items
41- `isRunning`: Whether the queuer is active and will process items automatically
42- `items`: Array of items currently waiting to be processed
43- `itemTimestamps`: Timestamps when items were added to the queue for expiration tracking
44- `lastResult`: The result from the most recent task execution
45- `pendingTick`: Whether the queuer has a pending timeout for processing the next item
46- `rejectionCount`: Number of items that have been rejected from being added to the queue
47- `settleCount`: Number of task executions that have completed (either successfully or with errors)
48- `size`: Number of items currently in the queue
49- `status`: Current processing status - 'idle' when not processing, 'running' when active, 'stopped' when paused
50- `successCount`: Number of task executions that have completed successfully
51
52## Options and ownership
53
54Pass an options object with property getters or a factory. Top-level properties are read
55reactively; function-valued core options remain callbacks. Local options override provider
56defaults. Updates retain the utility, its store, counters, and pending work.
57Destroying the component calls stop() and abort().
58onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
59must perform all required cleanup. Use flush() where supported to finish pending work.
60
61## Type Parameters
62
63### TValue
64
65`TValue`
66
67### TSelected
68
69`TSelected` = \{
70\}
71
72## Parameters
73
74### fn
75
76(`item`) => `Promise`\<`any`\>
77
78Function executed by the utility.
79
80### options?
81
82[`SveltePacerOptions`](../type-aliases/SveltePacerOptions.md)\<[`SvelteAsyncQueuerOptions`](../interfaces/SvelteAsyncQueuerOptions.md)\<`TValue`, `TSelected`\>\> = `{}`
83
84Core options or a reactive factory, plus an optional onUnmount callback.
85
86### selector?
87
88(`state`) => `TSelected`
89
90Selects state that updates the owner. Omit to leave selected state empty.
91
92## Returns
93
94[`SvelteAsyncQueuer`](../interfaces/SvelteAsyncQueuer.md)\<`TValue`, `TSelected`\>
95
96The retained utility instance with selected state and child subscriptions.
97
98## Example
99
100```ts
101import { createAsyncQueuer } from '@tanstack/svelte-pacer'
102
103const utility = createAsyncQueuer(
104 async (value: string) => { console.log(value) },
105 { wait: 100 },
106 (state) => ({ size: state.size }),
107)
108utility.addItem('item')
109// Selected state: utility.state.size
110```