Build Angular's linkedSignal and resource(): params, loader aborts, status values, reload and local set

hardAbout 60 minutesUpdated CodingProangularsignalsresourcelinked-signalasyncabortsignalrace-conditions
Not solved

Share this question

Problem

Angular 19 introduced two signal APIs that interviewers now ask about. linkedSignal is a writable signal that resets when another signal changes (a selected item that falls back to the first option when the list changes). resource() connects async work to signals: it runs a loader whenever its params change, cancels stale requests, and exposes the result as signals. Build both on top of the small signals core in the starter (keep the core as it is).

const userId = signal(7);
const user = resource({
  params: () => ({ id: userId() }),
  loader: ({ params, abortSignal }) => fetch(`/api/users/${params.id}`, { signal: abortSignal }).then((r) => r.json()),
});
user.status();   // "loading" (the loader has not run yet)
flushEffects();  // the loader runs
// ...later: user.status() === "resolved", user.value() is the user
userId.set(8);   // "loading" again; the next flush aborts the request for 7

Ref. UR-1451 · © UIRound · uiround.com/questions/build-angulars-linkedsignal-and-resource-params-loader-aborts-status-values-reload-and-local-set

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment