
Angular 22 released a stable version of the Resource API, providing a complete API to query an API,...
Angular 22 released a stable version of the Resource API, providing a complete API to query an API, now including loading/errors states builtin and some other features to improve our dev experience.
One place in Angular apps where we need to make API calls are Guards... and Resolvers.
They both block a current navigation to resolve their internal logic:
They both block the navigation until their logic is resolved. For this reason they obviously return a boolean, a Promise<boolean>, or an Observable<boolean>.
Resources are part of the new Signals API, meant to provide a new reactive solution for Angular apps. But as being reactive, they do not synchronously return a value.
When you create a Resource, its initial value is undefined while it immediately transitions into a loading state.
Let's look at what happens if we try to use a Resource inside a Guard:
export const authGuard: CanActivateFn = () => {
const authResource = httpResource<boolean>(() => ({
url: '/api/auth/status',
}));
// This returns the signal's current value synchronously
// Which is `undefined` initially!
return authResource.value() ?? false;
};
Because .value() is a Signal, reading it inside the Guard will synchronously return undefined (or whatever default value you provided). The Guard will immediately evaluate this to false and cancel the navigation instantly, without waiting for the API call to finish!
To make this work, you would have to jump through hoops to convert the Resource's status into an Observable, wait for it to stop loading, and then emit the value. This defeats the entire purpose of the simple Router Guard API.
Since Guards and Resolvers are meant to represent a single asynchronous event that blocks navigation, traditional Promises or RxJS Observables remain the perfect tool for the job.
Instead of fighting against the reactive nature of Signals, stick to the HttpClient returning an Observable (or the native fetch API returning a Promise):
export const authGuard: CanActivateFn = () => {
const http = inject(HttpClient);
// An Observable cleanly blocks the Router until it completes or emits
return http.get<boolean>('/api/auth/status');
};
The Resource API (resource and httpResource) is fantastic for components and services where you want to seamlessly bind async data to your templates while tracking loading and error states. However, Guards and Resolvers are fundamentally about blocking a process until a single async event completes.
For these Router mechanisms, Promises and Observables are precisely designed to handle that kind of control flow. Keep your Guards simple, and save the Resource API for your UI's reactive data needs!
aiMost of us have seen a coding agent fail to complete a task we know it can do. We just don't...
googlecloudWhen building Generative AI applications, developers often encounter a massive bottleneck: sequential...
discussI’ve been thinking about sharing some electronic circuit posts on Dev.to — small circuits, DIY...
agentsWhat nobody tells you about exporting your multi-agent prototype to a local workspace. Every...
agenticarchitectAutonomous agents are genuinely good at answering messy business questions. Give one an LLM and a set...
aiPR volume went up, ticket quality didn't, and the gap got filled with LLMs on both sides of the review: bots reviewing, bots replying, bots occasionally arguing with bots about priorities that only existed in a teammate's head. Our CEO named the actual problem, and it's bigger than code review.
Workflows from the Neura Market marketplace related to this Stable Diffusion resource