Task Controller
The Task controller manages async operations inside components such as data fetching, computations, or any promise-based work. It tracks loading, success, and error states automatically, cancels in-flight requests when args change, and provides a convenient render() helper for mapping states to templates.
It is a port of Lit's @lit/task: the constructor forms, the task function's arguments, autoRun, taskComplete, initialValue, argsEqual and the rest behave exactly as they do in Lit, so code written for Lit's Task runs unchanged once the import points at @webjsdev/core/task. The one WebJs addition: a task never runs during server rendering.
import { Task, TaskStatus, initialState, shallowArrayEquals, deepArrayEquals } from '@webjsdev/core/task'; Basic Usage
Create a Task inside a component. Pass a task function and an args function that returns the reactive inputs. When the args change, the task re-runs automatically.
The task function receives ONE array, the one args() returned, followed by an options object carrying the AbortSignal. Destructure the array: async ([userId], { signal }) => .... The arguments are never spread into separate parameters.
import { WebComponent, html } from '@webjsdev/core';
import { Task } from '@webjsdev/core/task';
class UserProfile extends WebComponent({ userId: String }) {
constructor() {
super();
this.userId = '';
}
#task = new Task(this, {
task: async ([userId], { signal }) => {
const res = await fetch(`/api/users/${userId}`, { signal });
if (!res.ok) throw new Error(`User not found (${res.status})`);
return res.json();
},
args: () => [this.userId],
});
render() {
return this.#task.render({
pending: () => html`<p>Loading user...</p>`,
complete: (user) => html`
<h2>${user.name}</h2>
<p>${user.email}</p>
`,
error: (err) => html`<p class="error">${err.message}</p>`,
});
}
}
UserProfile.register('user-profile'); The positional form new Task(this, taskFn, argsFn) is the same task without the config object:
#task = new Task(this, async ([userId], { signal }) => {
const res = await fetch(`/api/users/${userId}`, { signal });
return res.json();
}, () => [this.userId]); Configuration
| Option | Meaning |
|---|---|
task | The function to run, called as task(args, { signal }). Return (or resolve) initialState to put the task back into INITIAL, for example when the search box is empty. |
args | Returns the array of inputs. Without it the task never runs on its own, only through run(). |
autoRun | true (default) runs before the host renders when the args changed, 'afterUpdate' runs after the host rendered (so the task can read the new DOM), false only runs on run(). It is also a public field, so task.autoRun = false works later. |
argsEqual | Decides whether the args changed. Default shallowArrayEquals (same length, every element Object.is equal). Pass deepArrayEquals when args() builds fresh objects each update. |
initialValue | Starts the task COMPLETE with this value and records the current args, so it only runs once they change. |
onComplete / onError | Called with the value or the error when a run settles. A throw from either is ignored. |
Task States
A task is always in one of four states, exposed as TaskStatus constants:
| Status | Value | Meaning |
|---|---|---|
TaskStatus.INITIAL | 0 | Task has never run yet. This is the state before the first args evaluation. |
TaskStatus.PENDING | 1 | Task is currently running. An AbortController is active. |
TaskStatus.COMPLETE | 2 | Task resolved successfully. The result is available at task.value. |
TaskStatus.ERROR | 3 | Task rejected. The error is available at task.error. |
You can check the status directly:
render() {
if (this.#task.status === TaskStatus.PENDING) {
return html`<spinner-icon></spinner-icon>`;
}
if (this.#task.status === TaskStatus.ERROR) {
return html`<p>${this.#task.error.message}</p>`;
}
if (this.#task.status === TaskStatus.COMPLETE) {
return html`<p>${this.#task.value.name}</p>`;
}
return html`<p>Waiting...</p>`;
} task.render()
The render() helper provides a cleaner pattern. Pass an object with callbacks for each state:
this.#task.render({
initial: () => html`<p>Enter a search term</p>`,
pending: () => html`<p>Searching...</p>`,
complete: (data) => html`<result-list .items=${data}></result-list>`,
error: (err) => html`<p class="error">${err.message}</p>`,
}) All callbacks are optional. If a callback is not provided for the current state, nothing is rendered for that state.
AbortSignal Support
Every task run receives an AbortSignal via the second parameter. The signal is aborted when:
- A new run starts while one is pending (args changed, or
run()was called). The older run's result is dropped. - You call
task.abort(reason). The reason becomessignal.reason, and the rejection your task function produces (afetchrejects with it) lands as the ERROR state.
As in Lit, removing the host from the DOM does NOT abort a pending run, so an element that is moved keeps its result. If the work must stop when the element goes away, call this.#task.abort() in disconnectedCallback().
Pass the signal to fetch() or any other AbortSignal-aware API to cancel in-flight work:
#search = new Task(this, {
task: async ([query], { signal }) => {
// If the user types again before this resolves,
// the signal aborts and this fetch is cancelled.
const res = await fetch(`/api/search?q=${encodeURIComponent(query)}`, { signal });
return res.json();
},
args: () => [this.query.get()],
}); This prevents race conditions where an older, slower request resolves after a newer one, a common bug in naive async patterns.
autoRun Behavior
By default, the task runs automatically whenever the args function returns new values (compared via shallow equality). This is the autoRun: true behavior.
Set autoRun: false to control when the task runs manually:
#submit = new Task(this, {
task: async ([formData], { signal }) => {
const res = await fetch('/api/submit', {
method: 'POST',
body: JSON.stringify(formData),
signal,
});
return res.json();
},
args: () => [this.formData.get()],
autoRun: false, // only runs when you call .run()
});
handleSubmit(e) {
e.preventDefault();
this.#submit.run(); // manually trigger the task
} Use autoRun: false for tasks that should only fire on explicit user action: form submissions, delete confirmations, or manual refresh buttons. run(args) also accepts an explicit args array for that one run.
taskComplete
task.taskComplete is a promise for the current run: it resolves with the value or rejects with the error. With no run pending it settles straight away: it resolves with the current value, or rejects with the current error. Useful in tests and in code that needs the result after triggering a run:
this.userId = '42';
await this.updateComplete; // the update ran the task
const user = await this.#task.taskComplete; Server Rendering
A task never runs during server rendering. The server renders the INITIAL state (or the initialValue, as COMPLETE), and the browser runs the task when the component connects. Data that should be in the first paint belongs in an async render() or an async page function instead.
Reactive Args
The args function is called on every host update. When the returned array differs from the previous one (shallow comparison per element), the task re-runs. This creates a reactive chain: property change triggers host update, host update evaluates args, changed args trigger the task.
class SearchResults extends WebComponent({
query: prop(String),
page: prop(Number),
}) {
constructor() {
super();
this.query = '';
this.page = 1;
}
// Re-runs whenever query OR page changes
#results = new Task(this, {
task: async ([q, p], { signal }) => {
const res = await fetch(`/api/search?q=${q}&page=${p}`, { signal });
return res.json();
},
args: () => [this.query, this.page],
});
render() {
return this.#results.render({
pending: () => html`<p>Loading...</p>`,
complete: (data) => html`
<ul>${data.items.map(i => html`<li>${i.title}</li>`)}</ul>
<p>Page ${this.page} of ${data.totalPages}</p>
`,
error: (e) => html`<p>${e.message}</p>`,
});
}
}
SearchResults.register('search-results'); When to Use Task vs Async Page Functions
| Scenario | Use |
|---|---|
| Page-level data loading (blog posts, product details) | Async page function: runs on the server, included in the initial HTML. |
| Search-as-you-type, autocomplete | Task: client-side, reactive to user input, cancels stale requests. |
| Lazy-loaded component data (expand a section, scroll into view) | Task: client-side, runs on demand or on connect. |
| Form submission | Task with autoRun: false: fires on explicit user action. |
| Data shown on first paint (SEO-relevant) | Async page function: server-rendered HTML for crawlers. |
| Data that changes based on client-side state (filters, tabs) | Task: re-runs reactively when args change. |
The rule of thumb: if the data should be in the initial HTML (for SEO, performance, or because it doesn't depend on client state), use an async page function. If the data depends on user interaction or client-only state, use Task.
Full Example: Search With Debounce
import { WebComponent, html, css, signal } from '@webjsdev/core';
import { Task, initialState } from '@webjsdev/core/task';
class LiveSearch extends WebComponent {
static shadow = true;
static styles = css`
:host { display: block; }
input { width: 100%; padding: 8px; font: inherit; }
.results { margin-top: 8px; }
.empty { color: var(--fg-muted, #888); }
`;
readonly #query = signal('');
readonly #debounced = signal('');
_timer = null;
#results = new Task(this, {
task: async ([q], { signal }) => {
if (!q) return initialState;
const res = await fetch(`/api/search?q=${encodeURIComponent(q)}`, { signal });
if (!res.ok) throw new Error('Search failed');
return res.json();
},
args: () => [this.#debounced.get()],
});
onInput(e) {
const q = e.target.value;
this.#query.set(q);
clearTimeout(this._timer);
this._timer = setTimeout(() => {
this.#debounced.set(q);
}, 300);
}
disconnectedCallback() {
super.disconnectedCallback();
clearTimeout(this._timer);
}
render() {
return html`
<input placeholder="Search..."
.value=${this.#query.get()}
@input=${(e) => this.onInput(e)} />
<div class="results">
${this.#results.render({
initial: () => html`<p class="empty">Type to search</p>`,
pending: () => html`<p>Searching...</p>`,
complete: (items) => items.length === 0
? html`<p class="empty">No results</p>`
: html`<ul>${items.map(i => html`<li>${i.title}</li>`)}</ul>`,
error: (e) => html`<p class="error">${e.message}</p>`,
})}
</div>
`;
}
}
LiveSearch.register('live-search'); Next Steps
- Reactive Controllers: the general pattern Task is built on
- Context Protocol: share data across components without prop drilling
- Server Actions: server-side data fetching and mutations