mergeMap, what will be the order of console logs when the button is clicked twice quickly?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(); } }
When clicking twice quickly, mergeMap starts both inner observables without waiting. The synchronous console.log('clicked') runs twice immediately. Then after 1 second, both inner observables emit 'inner', so the logs show two 'clicked' first, then two 'inner'.
concatMap?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(); } }
Each click logs 'clicked' immediately. The concatMap queues the inner observables and runs them sequentially. So after all clicks, the inner observables emit 'inner' one by one, resulting in three 'inner' logs after the three 'clicked' logs.
exhaustMap, what will be the console output if the button is clicked three times quickly?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(); } }
On the first click, 'clicked' logs and the inner observable starts. Subsequent clicks log 'clicked' but their inner observables are ignored because exhaustMap ignores new inner observables until the current one completes. After 700ms, the single inner observable emits 'inner'.
mergeMap, concatMap, or exhaustMap in Angular?Option C uses curly braces without a return statement, so the arrow function returns undefined instead of the inner observable. This causes a runtime error. Options B, C, and D correctly return the inner observable.
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?concatMap waits for each inner observable to finish before starting the next, ensuring sequential execution. mergeMap subscribes to all inner observables immediately, allowing them to run in parallel. This difference explains the observed behavior.