0
0
Angularframework~20 mins

Effect for side effects in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Effect Side Effects Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Angular effect runs?

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?

Angular
readonly loginEffect = this.effect<string>((login$) => {
  return login$.pipe(
    tap(username => console.log(`User logged in: ${username}`))
  );
});
AIt modifies the username before passing it to the next effect in the chain.
BIt dispatches a new action with the username after logging it to the console.
CIt logs the username to the console whenever a login action occurs, without dispatching a new action.
DIt cancels the login action and prevents further processing.
Attempts:
2 left
💡 Hint

Think about what tap does in RxJS streams.

state_output
intermediate
2:00remaining
What is the output of this effect when triggered?

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?

Angular
readonly saveEffect = this.effect((save$) => {
  return save$.pipe(
    tap(() => this.notificationService.show('Saved successfully'))
  );
});
AThe notification service will display 'Saved successfully' each time the save action occurs, without dispatching new actions.
BThe effect will dispatch a new action with the notification message.
CNothing happens because the effect does not subscribe to the save$ stream.
DThe effect will cause a runtime error because it returns no observable.
Attempts:
2 left
💡 Hint

Remember that effects automatically subscribe to the observable returned.

📝 Syntax
advanced
2:00remaining
Which option correctly defines an effect for side effects only?

Choose the correct Angular effect syntax that performs a side effect without dispatching a new action.

Areadonly logEffect = this.effect((actions$) =&gt; actions$.pipe(tap(action =&gt; console.log(action))));
Breadonly logEffect = this.effect((actions$) =&gt; actions$.pipe(map(action =&gt; ({ type: 'LOG', payload: action }))));
Creadonly logEffect = this.effect((actions$) =&gt; actions$.pipe(filter(action =&gt; action.type === 'LOG')));
Dreadonly logEffect = this.effect((actions$) =&gt; actions$.pipe(reduce((acc, curr) =&gt; acc + 1, 0)));
Attempts:
2 left
💡 Hint

Look for the operator that performs side effects without changing the stream.

🔧 Debug
advanced
2:00remaining
Why does this effect not trigger the side effect?

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?

Angular
readonly effectWithoutReturn = this.effect((actions$) => {
  actions$.pipe(
    tap(() => console.log('Effect triggered'))
  );
});
ABecause console.log is asynchronous and does not run inside effects.
BBecause the effect function does not return the observable, so it is never subscribed to.
CBecause the effect should use map instead of tap to trigger side effects.
DBecause the tap operator is used incorrectly and does not cause side effects.
Attempts:
2 left
💡 Hint

Check if the observable is returned from the effect function.

🧠 Conceptual
expert
3:00remaining
What is the key difference between effects for side effects and effects that dispatch actions?

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?

AEffects for side effects automatically unsubscribe, but dispatching effects never unsubscribe.
BEffects for side effects use the map operator, while effects that dispatch actions use tap.
CEffects for side effects must be declared in the component, while dispatching effects are declared in services.
DEffects for side effects return an observable that completes without emitting actions, while effects that dispatch actions emit new actions to the store.
Attempts:
2 left
💡 Hint

Think about what the effect returns and what the store expects.