Skip to content

Commit 1b6b9e2

Browse files
atscottAndrewKushnir
authored andcommitted
docs: Add more pending tasks documentation in zoneless readme (angular#59689)
This commit expands on the pending tasks documentation in the zoneless readme PR Close angular#59689
1 parent d144c0e commit 1b6b9e2

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

adev/src/content/guide/zoneless.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,39 @@ are used in applications that still rely on ZoneJS.
8686

8787
If you are using SSR with Angular, you may know that it relies on ZoneJS to help determine when the application
8888
is "stable" and can be serialized. If there are asynchronous tasks that should prevent serialization, an application
89-
not using ZoneJS will need to make Angular aware of these with the `PendingTasks` service. Serialization
89+
not using ZoneJS must make Angular aware of these with the [PendingTasks](/api/core/PendingTasks) service. Serialization
9090
will wait for the first moment that all pending tasks have been removed.
9191

92+
93+
The two most straightforward uses of pending tasks are the `run` method:
94+
95+
```typescript
96+
const taskService = inject(PendingTasks);
97+
taskService.run(async () => {
98+
const someResult = await doSomeWorkThatNeedsToBeRendered();
99+
this.someState.set(someResult);
100+
});
101+
```
102+
103+
For more complicated use-cases, you can manuall add and remove a pending tasks:
104+
92105
```typescript
93106
const taskService = inject(PendingTasks);
94107
const taskCleanup = taskService.add();
95-
await doSomeWorkThatNeedsToBeRendered();
96-
taskCleanup();
108+
try {
109+
await doSomeWorkThatNeedsToBeRendered();
110+
} catch {
111+
// handle error
112+
} finally {
113+
taskCleanup();
114+
}
115+
```
116+
117+
In addition, the [pendingUntilEvent](/api/core/rxjs-interop/pendingUntilEvent#) helper in `rxjs-interop` ensures
118+
the application remains unstable until the observable emits, complets, errors, or is unsubscribed.
119+
120+
```typescript
121+
readonly myObservableState = someObservable.pipe(pendingUntilEvent());
97122
```
98123

99124
The framework uses this service internally as well to prevent serialization until asynchronous tasks are complete. These include, but are not limited to,

0 commit comments

Comments
 (0)