Explore key concepts of RxJS and Observables in Angular with this quiz designed to assess your understanding of streams, operators, subscriptions, and reactive patterns in modern web development. Strengthen your grasp on handling asynchronous data flows and best practices for Angular applications.
Which RxJS operator is commonly used to convert a promise into an Observable in Angular applications?
Explanation: The 'from' operator can convert various data types, including promises, into Observables, making it useful for integrating asynchronous operations in Angular. 'filter' is used to selectively allow emissions based on a condition, not for converting promises. 'tap' is for side effects and does not create Observables. 'forkJoin' combines multiple Observables but does not create one from a promise.
In Angular templates, why is the async pipe preferred over manual subscription to Observables?
Explanation: The async pipe subscribes to an Observable when the template is initialized and unsubscribes when the view is destroyed, which helps prevent memory leaks. It does not handle errors in silence but propagates them. Preventing syntax errors and eliminating HTTP requests are unrelated to what the async pipe does; those options do not reflect its actual purpose.
Which Subject type in RxJS stores the most recent value and emits it to new subscribers immediately upon subscription?
Explanation: BehaviorSubject requires an initial value and always returns the latest value to new subscribers. ReplaySubject can emit multiple previous values but behaves differently than BehaviorSubject. 'Subject' does not store any previous values, so new subscribers do not receive past emissions. 'AsyncSubject' only emits the last value upon completion, which is not the same behavior as BehaviorSubject.
When handling HTTP requests that depend on user input, which operator should you use to avoid unwanted emissions from previous requests?
Explanation: switchMap cancels any ongoing inner Observable when a new value is emitted, ensuring only the most recent request is processed—ideal for responding to user input changes. mergeMap and concatMap do not cancel previous requests and will process all emissions. The map operator only transforms values and does not handle Observables resulting from HTTP requests, making it unsuitable here.
Why is it important to unsubscribe from manually created Observable subscriptions in Angular components?
Explanation: Unsubscribing ensures that no lingering processes or handlers remain after a component is destroyed, which would otherwise lead to memory leaks. Unsubscribing does not speed up rendering, nor does it create new Observables or allow more emissions; those options misrepresent how subscriptions and memory management work.