0
0
Angularframework~20 mins

Observable in component lifecycle in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Observable Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an Observable is subscribed in ngOnInit but not unsubscribed?

Consider an Angular component that subscribes to an Observable inside ngOnInit but does not unsubscribe in ngOnDestroy. What is the likely outcome?

Angular
import { Component, OnInit } from '@angular/core';
import { interval } from 'rxjs';

@Component({
  selector: 'app-timer',
  template: `<p>Timer running</p>`
})
export class TimerComponent implements OnInit {
  ngOnInit() {
    interval(1000).subscribe(val => console.log(val));
  }
}
AThe Observable throws an error when the component is destroyed.
BThe Observable automatically stops emitting when the component is destroyed.
CThe Observable will keep emitting even after the component is destroyed, causing a memory leak.
DThe Observable completes immediately after the component is initialized.
Attempts:
2 left
💡 Hint

Think about what happens to subscriptions if you don't clean them up.

state_output
intermediate
2:00remaining
What is the console output when using takeUntil in ngOnDestroy?

Given the following Angular component code, what will be printed to the console when the component is destroyed after 3 seconds?

Angular
import { Component, OnInit, OnDestroy } from '@angular/core';
import { interval, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-example',
  template: `<p>Example</p>`
})
export class ExampleComponent implements OnInit, OnDestroy {
  private destroy$ = new Subject<void>();

  ngOnInit() {
    interval(1000)
      .pipe(takeUntil(this.destroy$))
      .subscribe(val => console.log(val));

    setTimeout(() => this.ngOnDestroy(), 3000);
  }

  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
    console.log('Destroyed');
  }
}
A
Destroyed
0
1
2
B
0
1
2
Destroyed
C
0
1
2
3
Destroyed
D
0
1
Destroyed
Attempts:
2 left
💡 Hint

Consider how takeUntil stops the subscription when destroy$ emits.

🔧 Debug
advanced
2:00remaining
Why does this Angular component fail to unsubscribe properly?

Examine the following Angular component code. Why does the subscription not stop when the component is destroyed?

Angular
import { Component, OnInit, OnDestroy } from '@angular/core';
import { interval, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-buggy',
  template: `<p>Buggy</p>`
})
export class BuggyComponent implements OnInit, OnDestroy {
  private destroy$ = new Subject<void>();

  ngOnInit() {
    interval(1000)
      .pipe(takeUntil(this.destroy$))
      .subscribe(val => console.log(val));
  }

  ngOnDestroy() {
    this.destroy$ = new Subject<void>();
    this.destroy$.next();
    this.destroy$.complete();
  }
}
AThe destroy$ Subject is reassigned in ngOnDestroy, so the original subscription never receives the next notification.
BThe subscription is missing the unsubscribe call, so it never stops.
CThe interval Observable does not emit any values, so the subscription never starts.
DThe component is missing the OnDestroy interface implementation.
Attempts:
2 left
💡 Hint

Look carefully at how destroy$ is used in ngOnDestroy.

🧠 Conceptual
advanced
2:00remaining
Which RxJS operator is best to automatically unsubscribe on component destroy?

In Angular, to avoid manual unsubscription, which RxJS operator is designed to complete an Observable when a notifier emits, commonly used with a Subject triggered in ngOnDestroy?

AtakeUntil
BswitchMap
CmergeMap
Dfilter
Attempts:
2 left
💡 Hint

This operator listens to a notifier Observable and completes the source Observable when the notifier emits.

📝 Syntax
expert
2:00remaining
Identify the syntax error preventing proper Observable unsubscription

Which option contains a syntax error that will prevent the Angular component from compiling and unsubscribing correctly?

Angular
import { Component, OnInit, OnDestroy } from '@angular/core';
import { interval, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-syntax',
  template: `<p>Syntax Test</p>`
})
export class SyntaxComponent implements OnInit, OnDestroy {
  private destroy$ = new Subject<void>();

  ngOnInit() {
    interval(1000)
      .pipe(takeUntil(this.destroy$))
      .subscribe(val => console.log(val));
  }

  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
}
A
ngOnDestroy() {
  this.destroy$.next();
  this.destroy$.complete();
  return;
}
B
ngOnDestroy() {
  this.destroy$.next();
  this.destroy$.complete();
}
C
ngOnDestroy() {
  this.destroy$.next();
  this.destroy$.complete()
}
D
ngOnDestroy() {
  this.destroy$.next()
  this.destroy$.complete()
}
Attempts:
2 left
💡 Hint

Check for missing semicolons that cause syntax errors in TypeScript.