0
0
Angularframework~20 mins

tap operator for side effects in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RxJS Tap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does the tap operator do in this Angular RxJS code?
Consider this Angular service method using RxJS:
getData() {
  return this.http.get('/api/data').pipe(
    tap(data => console.log('Data received:', data))
  );
}

What is the main purpose of the tap operator here?
Angular
getData() {
  return this.http.get('/api/data').pipe(
    tap(data => console.log('Data received:', data))
  );
}
AIt modifies the data before passing it to subscribers.
BIt delays the data emission by a fixed time.
CIt filters out unwanted data from the stream.
DIt performs a side effect without changing the data stream.
Attempts:
2 left
💡 Hint
Think about whether tap changes the data or just observes it.
state_output
intermediate
2:00remaining
What will be logged and emitted by this observable?
Given this Angular RxJS snippet:
of(1, 2, 3).pipe(
  tap(x => console.log('Side effect:', x)),
  map(x => x * 10)
).subscribe(result => console.log('Result:', result));

What will be the console output sequence?
Angular
of(1, 2, 3).pipe(
  tap(x => console.log('Side effect:', x)),
  map(x => x * 10)
).subscribe(result => console.log('Result:', result));
A
Side effect: 10
Side effect: 20
Side effect: 30
Result: 10
Result: 20
Result: 30
B
Side effect: 1
Result: 10
Side effect: 2
Result: 20
Side effect: 3
Result: 30
C
Result: 10
Result: 20
Result: 30
D
Side effect: 1
Result: 1
Side effect: 2
Result: 2
Side effect: 3
Result: 3
Attempts:
2 left
💡 Hint
Remember tap runs before map and does not change values.
📝 Syntax
advanced
2:00remaining
Which option correctly uses tap to update a loading flag?
You want to set loading = true before an HTTP call and loading = false after it completes using tap. Which code snippet is correct?
Angular
loading = false;

fetchData() {
  this.loading = true;
  this.http.get('/api/items').pipe(
    tap({
      next: () => this.loading = false,
      error: () => this.loading = false
    })
  ).subscribe();
}
Atap({ next: () => this.loading = false, error: () => this.loading = false })
Btap(() => this.loading = false)
Ctap(data => this.loading = true)
Dtap({ complete: () => this.loading = true })
Attempts:
2 left
💡 Hint
Consider how to handle success and error to reset loading.
🔧 Debug
advanced
2:00remaining
Why does this tap usage cause a runtime error?
Examine this Angular RxJS code:
this.http.get('/api/data').pipe(
  tap(data => { this.processData(data); })
).subscribe();

Inside processData, this is undefined. Why?
Angular
this.http.get('/api/data').pipe(
  tap(data => { this.processData(data); })
).subscribe();
ABecause the function passed to tap is not bound to the component instance.
BBecause tap runs outside Angular zone causing this to be undefined.
CBecause arrow functions do not bind their own this, so this is undefined.
DBecause processData is not declared as an arrow function.
Attempts:
2 left
💡 Hint
Think about how this works inside callbacks.
🧠 Conceptual
expert
2:00remaining
What is the key difference between tap and map in RxJS?
Choose the statement that best describes the difference between tap and map operators in RxJS.
Atap transforms the data stream, map performs side effects without changing data.
Btap delays emissions, map filters data based on a condition.
Ctap performs side effects without changing data, map transforms the data stream.
DBoth tap and map transform data but tap also logs it.
Attempts:
2 left
💡 Hint
One changes data, the other just observes it.