0
0
Angularframework~20 mins

mergeMap vs concatMap vs exhaustMap in Angular - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RxJS Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Behavior of mergeMap with overlapping inner observables
Given the following Angular component code using mergeMap, what will be the order of console logs when the button is clicked twice quickly?
Angular
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subject, of } from 'rxjs';
import { delay, mergeMap, takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-test',
  template: `<button (click)="clickHandler()">Click me</button>`
})
export class TestComponent implements OnInit, OnDestroy {
  private clicks = new Subject<void>();
  private destroy$ = new Subject<void>();

  ngOnInit() {
    this.clicks.pipe(
      mergeMap(() => of('inner').pipe(delay(1000))),
      takeUntil(this.destroy$)
    ).subscribe(value => console.log(value));
  }

  clickHandler() {
    console.log('clicked');
    this.clicks.next();
  }

  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
}
Aclicked, clicked, inner, inner
Bclicked, inner, clicked, inner
Cinner, inner, clicked, clicked
Dinner, clicked, inner, clicked
Attempts:
2 left
💡 Hint
Remember that mergeMap subscribes to all inner observables immediately and runs them concurrently.
component_behavior
intermediate
2:00remaining
concatMap behavior with multiple rapid clicks
What will be the console output order when clicking the button three times quickly in this Angular component using concatMap?
Angular
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subject, of } from 'rxjs';
import { delay, concatMap, takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-test',
  template: `<button (click)="clickHandler()">Click me</button>`
})
export class TestComponent implements OnInit, OnDestroy {
  private clicks = new Subject<void>();
  private destroy$ = new Subject<void>();

  ngOnInit() {
    this.clicks.pipe(
      concatMap(() => of('inner').pipe(delay(500))),
      takeUntil(this.destroy$)
    ).subscribe(value => console.log(value));
  }

  clickHandler() {
    console.log('clicked');
    this.clicks.next();
  }

  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
}
Ainner, clicked, inner, clicked, inner, clicked
Bclicked, clicked, clicked, inner, inner, inner
Cclicked, inner, clicked, inner, clicked, inner
Dclicked, inner, inner, inner, clicked, clicked
Attempts:
2 left
💡 Hint
concatMap queues inner observables and runs them one after another.
component_behavior
advanced
2:00remaining
exhaustMap behavior with rapid clicks
In this Angular component using exhaustMap, what will be the console output if the button is clicked three times quickly?
Angular
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subject, of } from 'rxjs';
import { delay, exhaustMap, takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-test',
  template: `<button (click)="clickHandler()">Click me</button>`
})
export class TestComponent implements OnInit, OnDestroy {
  private clicks = new Subject<void>();
  private destroy$ = new Subject<void>();

  ngOnInit() {
    this.clicks.pipe(
      exhaustMap(() => of('inner').pipe(delay(700))),
      takeUntil(this.destroy$)
    ).subscribe(value => console.log(value));
  }

  clickHandler() {
    console.log('clicked');
    this.clicks.next();
  }

  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
}
Aclicked, inner, clicked, inner, clicked, inner
Binner, clicked, clicked, clicked
Cclicked, inner, inner, clicked, clicked
Dclicked, clicked, clicked, inner
Attempts:
2 left
💡 Hint
exhaustMap ignores new inner observables while one is active.
📝 Syntax
advanced
2:00remaining
Identify the incorrect usage of mergeMap, concatMap, or exhaustMap
Which option contains a syntax or usage error in applying mergeMap, concatMap, or exhaustMap in Angular?
Asource$.pipe(exhaustMap(value => innerObservable(value)))
Bsource$.pipe(concatMap(value => innerObservable(value)))
Csource$.pipe(mergeMap(value => { innerObservable(value); }))
Dsource$.pipe(mergeMap(value => innerObservable(value)))
Attempts:
2 left
💡 Hint
Check if the arrow function returns the inner observable correctly.
🔧 Debug
expert
2:00remaining
Debugging unexpected behavior with concatMap and mergeMap
An Angular developer notices that when using concatMap in their code, inner observables run one after another, but when switching to mergeMap, all inner observables run at once. Which explanation best describes why this happens?
A<code>concatMap</code> queues inner observables and waits for each to complete before starting the next, while <code>mergeMap</code> subscribes to all inner observables immediately and runs them concurrently.
B<code>mergeMap</code> queues inner observables and waits for each to complete, while <code>concatMap</code> runs all inner observables concurrently.
C<code>concatMap</code> cancels previous inner observables when a new one starts, but <code>mergeMap</code> ignores new inner observables while one is active.
D<code>mergeMap</code> only runs the first inner observable and ignores the rest, while <code>concatMap</code> runs all inner observables concurrently.
Attempts:
2 left
💡 Hint
Think about how each operator handles multiple inner observables.