Sub topic 13
Chaining dependent resources
This chapter assumes you know how to create a resource() with a reactive params function, and that you can read value(), status(), and hasValue() on it. Here you will make one resource wait for another.
The problem
Some data depends on other data. To load a user's posts, you first need the user. You could pass the same userId signal to both resources, but that fails when the second request needs a value that comes from the first response, such as an organization ID inside the user record.
A resource skips its loader when params returns undefined. In that case it stays in the idle status and value() stays undefined, unless you set a defaultValue. You can use this to hold a second resource back until its input exists.
A dependent resource
import { Component, resource, signal } from '@angular/core';
interface User {
id: number;
name: string;
}
interface Post {
id: number;
title: string;
}
@Component({
selector: 'app-user-posts',
template: `<p>{{ posts.value()?.length ?? 0 }} posts</p>`,
})
export class UserPostsComponent {
readonly userId = signal(1);
readonly user = resource({
params: () => ({ id: this.userId() }),
loader: async ({ params, abortSignal }) => {
const res = await fetch(`/api/users/${params.id}`, { signal: abortSignal });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return (await res.json()) as User;
},
});
readonly posts = resource({
params: () => (this.user.hasValue() ? { userId: this.user.value().id } : undefined),
loader: async ({ params, abortSignal }) => {
const res = await fetch(`/api/users/${params.userId}/posts`, { signal: abortSignal });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return (await res.json()) as Post[];
},
});
}The posts params function reads this.user.hasValue(), so it depends on the user resource like any other signal. While user is loading for the first time, hasValue() is false, params returns undefined, and posts stays idle. When user resolves, params returns an object, and the posts loader runs.
hasValue() also narrows the type. Inside the true branch, this.user.value() is User and not User | undefined, so you can read .id without a check.
What happens when the first input changes
When userId changes, user starts a new load. Its status changes to loading (or reloading if it already had a value). The value() of a resource in loading is undefined, so hasValue() returns false. The posts params then returns undefined again, and the resource in progress is cancelled through its abortSignal. After the new user arrives, posts loads again.
Common errors
- Reading
value()when it has no value. Callingthis.user.value()while the resource is inerrorthrows. It also throws in other states that have no value, depending on your Angular version. Guard withhasValue()as shown. - Showing stale data. If
postsstill holds the previous user's posts while the new user loads, a template reading onlyposts.value()shows old data. Checkposts.isLoading()orposts.status()too. - Sequential latency. The two requests run one after the other. If the second request only needs
userId, give both resources the same signal and let them run in parallel. - Forgetting the idle state. An idle resource is neither loading nor failed. A template that shows "Loading..." only when
isLoading()is true will show nothing for a waiting resource, which is usually the right behavior but worth testing.
Exercise
Exercise
A comments resource needs the id of a post resource. Write the params function so that comments does not load until post has a value.
Show answer
params: () => (this.post.hasValue() ? { postId: this.post.value().id } : undefined). Returning undefined keeps comments idle, and hasValue() narrows the type so .id is safe to read.
Exercise
Why is chaining two resources slower than giving both the same signal, and when is chaining still the right choice?
Show answer
Chained resources run one after the other, so total time is the sum of both requests. Parallel resources sharing a signal run at the same time. Chain only when the second request needs data from the first response.