Sub topic 10
PendingTasks and application stability
This chapter assumes you can build standalone components, use signals, and write async code with Promises. You should also know how to enable zoneless mode, which earlier chapters covered.
What "stable" means without zones
Angular calls an application stable when it has no outstanding work that could still change the view. Two features depend on this signal:
- Server-side rendering (SSR) waits for stability before it serializes the page to HTML.
- Tests that call
fixture.whenStable()wait for stability before they continue.
In a zone-based app, Zone.js tracked pending macrotasks such as setTimeout and in-flight requests, and stability followed from that. A zoneless app has no such tracking. Angular still knows about its own work, such as scheduled change detection and requests made through HttpClient. It cannot see a bare fetch() call or a custom timer.
If Angular cannot see your async work, it may report the app as stable too early. SSR then sends HTML without the data, and tests assert before the data arrives.
Registering work with PendingTasks
PendingTasks is a service that lets you tell Angular that some work is still running. The simplest way to use it is run(). It takes an async function, marks a task as pending, and removes the mark when the returned Promise settles.
import { Injectable, inject, PendingTasks } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class DataLoader {
private readonly pendingTasks = inject(PendingTasks);
load(url: string) {
return this.pendingTasks.run(async () => {
const response = await fetch(url);
return response.json();
});
}
}run() returns a Promise that resolves with the function's result. Callers use load() as they would any async method. While the fetch is in flight, Angular treats the app as unstable.
Why this matters for tests
In a test, await fixture.whenStable() returns once no pending tasks remain and no change detection is scheduled. If your service wraps its fetch in PendingTasks.run(), the test waits for the data and then checks the rendered result. Without the wrapper, whenStable() can resolve first and the assertion sees an empty view.
The same rule applies to SSR. Anything the server must wait for before rendering should be inside a pending task.
Adding and removing tasks manually
When the work does not fit one async function, use add(). It returns a function that you call when the work is done.
import { Injectable, inject, PendingTasks } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class Poller {
private readonly pendingTasks = inject(PendingTasks);
waitOnce(ms: number): Promise<void> {
const removeTask = this.pendingTasks.add();
return new Promise<void>((resolve) => {
setTimeout(() => {
removeTask();
resolve();
}, ms);
});
}
}Every add() needs a matching call to the returned function. If an error skips that call, the task never ends. Use try/finally when the work can throw.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| SSR output has empty data | A raw `fetch` or timer is not registered | Wrap it in `PendingTasks.run()` |
| Test passes only with an extra `setTimeout` wait | Async work is invisible to `whenStable()` | Register the work as a pending task |
| SSR or `whenStable()` never finishes | A task was added and never removed | Use `try`/`finally`, or switch to `run()` |
Exercise
A service calls fetch() directly and stores the result in a signal. A zoneless SSR page renders with the signal still empty. Why does this happen, and how do you fix it?
Show answer
Angular cannot see the fetch() call, so it considers the app stable before the response arrives and serializes the page too early. Wrap the call in PendingTasks.run() so Angular keeps the app unstable until the Promise settles. The signal is then set before rendering finishes.
Exercise
Why is run() usually safer than add()?
Show answer
run() removes the task automatically when the Promise settles, including when it rejects. With add() you must call the returned function yourself, and a missed call (for example after a thrown error) leaves the app unstable forever.