Consider this Angular effect that listens for a login action and triggers a side effect:
readonly loginEffect = this.effect<string>((login$) => {
return login$.pipe(
tap(username => console.log(`User logged in: ${username}`))
);
});What is the main behavior of this effect?
readonly loginEffect = this.effect<string>((login$) => { return login$.pipe( tap(username => console.log(`User logged in: ${username}`)) ); });
Think about what tap does in RxJS streams.
The tap operator is used for side effects like logging. This effect listens to the login stream and logs the username but does not dispatch any new actions.
Given this Angular effect that triggers a notification service on a save action:
readonly saveEffect = this.effect((save$) => {
return save$.pipe(
tap(() => this.notificationService.show('Saved successfully'))
);
});What will happen when saveEffect runs?
readonly saveEffect = this.effect((save$) => { return save$.pipe( tap(() => this.notificationService.show('Saved successfully')) ); });
Remember that effects automatically subscribe to the observable returned.
The effect uses tap to call the notification service for side effects. Effects automatically subscribe, so the notification shows on each save.
Choose the correct Angular effect syntax that performs a side effect without dispatching a new action.
Look for the operator that performs side effects without changing the stream.
Option A uses tap which performs side effects without emitting new actions. Other options transform or filter the stream, which may dispatch actions or change behavior.
Look at this Angular effect:
readonly effectWithoutReturn = this.effect((actions$) => {
actions$.pipe(
tap(() => console.log('Effect triggered'))
);
});Why does the console log never appear when the effect runs?
readonly effectWithoutReturn = this.effect((actions$) => { actions$.pipe( tap(() => console.log('Effect triggered')) ); });
Check if the observable is returned from the effect function.
Effects must return an observable to be subscribed to. Without returning, the pipe is created but never executed, so side effects do not run.
In Angular, effects can either perform side effects only or dispatch new actions. What is the main difference in how these effects are defined and behave?
Think about what the effect returns and what the store expects.
Side effect-only effects return observables that do not emit actions, often using tap. Effects that dispatch actions emit new actions to the store, usually using map or similar operators.