Full answer
This is a classic puzzle. The class has count = signal(0), double = computed(...) that logs "computed", and an effect that logs the value of double() and count(). A method then does count.set(1); count.set(2); count.set(3);.
Rules to apply:
- Writing to a signal is immediate and synchronous. After
count.set(1),count()returns 1 right away. - A
computedis lazy. Changingcountonly marks the computed as stale. The function runs when somebody readsdouble(), and then the result is cached until the next change. Reading it twice with no change in between runs the function once. - An
effectis scheduled. When a signal it read changes, Angular marks the effect dirty and runs it later, as part of change detection (for component effects, before the component's template is checked). It does not run in the middle of your method. So three writes in a row give one effect run, withcount() === 3. - An effect always runs once on creation, after the first change detection pass, not inside the constructor.
- If you set the same value that the signal already has (
count.set(3)when it is 3), nothing is notified, because signals compare withObject.is. - In a component test,
fixture.detectChanges()runs the component's pending effects. For root effects in services, useTestBed.tick().
So the output for the example is: the effect runs once at start with count 0, and after the three writes and one flush it runs once with count 3 (and double computed once for the new value). The intermediate values 1 and 2 are never seen by the effect.
Practical lessons: do not rely on an effect to see every value. If every value matters (for example, analytics for each click), use an event handler or an RxJS stream. Do not write to signals inside a computed. Avoid chains of effects that set signals for other effects. Use computed or linkedSignal for derived state.
import { Component, computed, effect, signal } from '@angular/core';
// Captured log (Karma, Angular 20.3): render, then threeWrites(), then one change detection pass:
// computed, effect: count=0 double=0, after writes, computed, effect: count=3 double=6
export const log: string[] = [];
@Component({ selector: 'app-puzzle', template: `{{ count() }}` })
export class PuzzleComponent {
readonly count = signal(0);
readonly double = computed(() => {
log.push('computed');
return this.count() * 2;
});
constructor() {
effect(() => {
log.push(`effect: count=${this.count()} double=${this.double()}`);
});
}
threeWrites(): void {
this.count.set(1);
this.count.set(2);
this.count.set(3);
log.push('after writes');
}
}Angular 20.3, strict template check passes. A Karma test captured the real log, shown in the comment: the effect ran once at start and once after the three writes, and the computed ran once per change.
Key points
- Signal writes are synchronous. Effects run later and see the latest value only.
- Computed values are lazy and cached.
- Several writes before the flush give one effect run.
- Setting an equal value does not notify.
Common mistakes
- Expecting an effect to run for each of several writes.
- Expecting a computed to run when its source changes, before anyone reads it.
- Using effects to derive state.
Follow-ups
- How do you make an effect see every value?
- When exactly does a component effect run?
- What does `untracked` do inside an effect?
Tips
- Walk through the rules numerically, step by step.
- State that effects are scheduled and computed are lazy.
- Mention how you flush effects in tests.
Evaluation rubric
- Predicts the output correctly.
- Explains laziness, caching and scheduling.
- Draws a practical lesson.