You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/docs/src/routes/docs/(qwik)/core/tasks/index.mdx
+14-21Lines changed: 14 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ contributors:
20
20
- aendel
21
21
- jemsco
22
22
- varixo
23
-
updated_at: '2025-10-31T03:33:22Z'
23
+
updated_at: '2025-11-08T03:33:22Z'
24
24
created_at: '2023-03-31T02:40:50Z'
25
25
---
26
26
@@ -35,16 +35,16 @@ Tasks are meant for running asynchronous operations as part of component initial
35
35
>
36
36
> - Tasks are asynchronous.
37
37
> - Tasks run on server and browser.
38
-
> - Tasks run before initial rendering and block the initial render.
39
-
> - Subsequent task re-executions (when tracked state changes) don't block rendering by default, but can be configured to block DOM updates with `deferUpdates: true`.
38
+
> - Tasks run before rendering and can block rendering.
39
+
> - Subsequent task re-executions (when tracked state changes) block rendering by default, but can be configured to not block DOM updates with `deferUpdates: false`.
40
40
41
41
`useTask$()` should be your default go-to API for running either synchronous or asynchronous work as part of component initialization or state change. It is only when you can't achieve what you need with `useTask$()` that you should consider using `useVisibleTask$()` or `useResource$()`.
42
42
43
43
The basic use case for `useTask$()` is to perform work on component initialization. `useTask$()` has these properties:
44
44
- It can run on either the server or in the browser.
45
45
- It runs before the initial rendering and blocks the initial render.
46
46
- If multiple tasks are running then they are run sequentially in the order they were registered. An asynchronous task will block the next task from running until it completes.
47
-
- By default, subsequent re-executions (when tracked state changes) do not block DOM updates unless `deferUpdates: true` is specified.
47
+
- By default, subsequent re-executions (when tracked state changes) do block DOM updates unless `deferUpdates: false` is specified.
48
48
49
49
Tasks can also be used to perform work when a component state changes. In this case, the task will rerun every time the tracked state changes. See: [`track()`](#track).
50
50
@@ -111,19 +111,12 @@ interface TaskOptions {
111
111
The `deferUpdates` option controls whether subsequent task re-executions (when tracked state changes) should block DOM updates.
112
112
113
113
**Default behavior (`deferUpdates: false` or not specified):**
114
-
-**Initial render**: The task blocks rendering (runs before the component renders for the first time)
115
-
-**Subsequent runs**: The task runs asynchronously without blocking DOM updates
116
-
117
-
**With `deferUpdates: true`:**
118
114
-**Initial render**: The task blocks rendering (same as default)
119
115
-**Subsequent runs**: The task blocks DOM updates until it completes - the journal flush is deferred until the task finishes
120
116
121
-
**When to use `deferUpdates: true`:**
122
-
- When you need to ensure that asynchronous state updates complete before the UI reflects changes
123
-
- When you want to prevent visual flickering by ensuring all related state updates happen atomically
124
-
125
-
**Performance Considerations:**
126
-
- Use carefully as blocking DOM updates on subsequent runs can impact perceived performance
117
+
**With `deferUpdates: true`:**
118
+
-**Initial render**: The task blocks rendering (runs before the component renders for the first time)
119
+
-**Subsequent runs**: The task runs asynchronously without blocking DOM updates
127
120
128
121
Additionally, this task can be reactive and will re-execute when **tracked**[state](/docs/(qwik)/core/state/index.mdx) changes.
129
122
@@ -142,7 +135,7 @@ Additionally, this task can be reactive and will re-execute when **tracked** [st
142
135
143
136
> If `useTask$()` does not track any state, it will run **exactly once**, either in the server **or** in the browser (**not both**), depending where the component is initially rendered. Effectively behaving like an "on-mount" hook.
144
137
145
-
`useTask$()` will block the initial rendering of the component until after its async callback resolves. Tasks execute sequentially even if they are asynchronous (only one task executes at a time within a component). Subsequent re-executions (when tracking state changes) run asynchronously by default and don't block rendering unless `deferUpdates: true` is set.
138
+
`useTask$()` will block the rendering of the component until after its async callback resolves. Tasks execute sequentially even if they are asynchronous (only one task executes at a time within a component). Subsequent re-executions (when tracking state changes) run asynchronously by default and don't block rendering unless `deferUpdates: true` is set.
146
139
147
140
Take a look at the simplest use case of the task to run some asynchronous work on component initialization:
> - The `useTask$()` computes the fibonacci number one entry per 100 ms. So 40 entries take 4 seconds to compute.
169
+
> - The `useTask$()` computes the fibonacci number one entry per 100 ms. So 40 entries take 4 seconds to render.
177
170
> - The `useTask$()` executes on the server as part of the SSR (the result may be cached in CDN.)
178
-
> - Because the `useTask$()` blocks initial rendering, the rendered HTML page takes 4 seconds to generate.
179
-
> - Because this task has no `track()` it will never rerun, making it effectively initialization code.
171
+
> - Because the `useTask$()` blocks rendering, the rendered HTML page takes 4 seconds to render.
172
+
> - Because this task has no `track()` it will never rerun, making it effectively an initialization code.
180
173
> - Because this component only renders on the server, the `useTask$()` will never be downloaded or run on the browser.
181
174
182
175
> Notice that `useTask$()` runs on the server **BEFORE** the actual rendering. Therefore if you need to do DOM manipulation, use [`useVisibleTask$()`](#usevisibletask) instead, which runs on the browser after rendering.
183
176
184
177
Use `useTask$()` when you need to:
185
-
- Run async tasks before initial rendering
178
+
- Run async tasks before rendering
186
179
- Run code only once before the component is first rendered
187
-
- Programmatically run side-effect code when state changes (with or without blocking DOM updates)
180
+
- Programmatically run side-effect code when state changes
188
181
189
182
> Note, if you're thinking about loading data using `fetch()` inside of `useTask$`, consider using [`useResource$()`](/docs/core/state/#useresource) instead. This API is more efficient in terms of leveraging SSR streaming and parallel data fetching.
> -By default, `useTask$()` blocks the initial rendering until it completes. Subsequent executions (when tracked state changes) don't block rendering by default. In this example, we don't await `delay()`to avoid blocking even the initial render - the delay runs independently while the task completes immediately.
262
+
> -The `useTask$()` blocks rendering until it completes. If you don't want to block rendering, make sure that the task is resolved, and run the delay work on a separate unconnected promise. In this example, we don't await `delay()`because it would block rendering.
270
263
271
264
> Sometimes it is required to only run code either in the server or in the client. This can be achieved by using the `isServer` and `isBrowser` booleans exported from `@qwik.dev/core` as shown above.
0 commit comments