WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Angular · Guru · question 94 of 100

How can you create and use custom RxJS operators and utilities to streamline complex asynchronous operations in Angular applications?

📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

Creating and using custom RxJS operators and utilities in Angular can help improve the efficiency and readability of your code when working with complex asynchronous operations.

First, let’s review some basics. RxJS is a library for Reactive Programming using Observables to work with asynchronous data streams. Operators are functions that you can use to manipulate these streams, and custom operators allow you to create unique, reusable transformations for your specific needs.

To create and use custom RxJS operators and utilities in Angular, follow these steps:

1. Create a custom operator:

To create a custom operator, you define a function that takes a source observable as input and returns a new observable. The custom operator can be used with the ‘pipe()‘ method provided by RxJS observables.

Here is an example of a simple custom operator that multiplies each value emitted by a source observable by a given factor:

import { Observable } from 'rxjs';

function multiply(factor: number) {
  return (source: Observable<number>): Observable<number> => {
    return new Observable(observer => {
      const subscription = source.subscribe({
        next(value) {
          observer.next(value * factor);
        },
        error(error) {
          observer.error(error);
        },
        complete() {
          observer.complete();
        },
      });

      return () => {
        subscription.unsubscribe();
      };
    });
  };
}

2. Use the custom operator in an Angular component:

Now that you have created a custom operator, you can use it in an Angular component using the ‘pipe()‘ method on an Observable. Here’s an example on how to do this:

import { Component, OnInit } from '@angular/core';
import { interval } from 'rxjs';
import { multiply } from './multiply.operator';

@Component({
  selector: 'app-root',
  template: `
    <div *ngFor="let value of values">{{ value }}</div>
  `,
})
export class AppComponent implements OnInit {
  values: number[] = [];

  ngOnInit() {
    const source$ = interval(1000); // Emit values every 1 second
    const multiplied$ = source$.pipe(multiply(2));

    multiplied$.subscribe(value => {
      this.values.push(value);
    });
  }
}

In this example, we import the custom ‘multiply()‘ operator and use it to transform the ‘source$‘ Observable to emit values multiplied by 2. The transformed ‘multiplied$‘ Observable will emit new values every 1 second, and these values will be pushed to the ‘values‘ array and displayed in the component’s template.

By creating and using custom RxJS operators and utilities, you can efficiently handle complex asynchronous operations and make your Angular application’s code more modular, maintainable, and easy to understand.

Keep in mind, custom RxJS operators are powerful tools, but also try to utilize the built-in RxJS operators when possible. They cover various transformation, filtering, and chaining needs and can often solve or simplify tasks without the need for custom implementations.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Angular interview — then scores it.
📞 Practice Angular — free 15 min
📕 Buy this interview preparation book: 100 Angular questions & answers — PDF + EPUB for $5

All 100 Angular questions · All topics