Explain Codes LogoExplain Codes Logo

Return an empty Observable

javascript
observable
rxjs
typescript
Alex KataevbyAlex Kataev·Mar 5, 2025
⚡TLDR

Create an Observable that states its emptiness outright using the EMPTY constant from RxJS:

import { EMPTY } from 'rxjs'; // Like a vestigial tail of an evolution long past // Completes immediately with fanfare but, alas, emits nothing const silentButDeadly$ = EMPTY;

Conjure up silentButDeadly$ whenever the occasion calls for an Observable that emits null and takes a bow right away.

A practical guide to empty Observables

An empty Observable is your go-to in scenarios craving that closure without the burden of emitting any items. It gives you a clean slate to work with, every time.

Empty Observables in the wild

  • The Unfavourable Condition: Use EMPTY to elegantly sidestep needless operations.
  • The Place of Placeholder: In lieu of an inherently dynamic Observable stream, not always privileged with values.
  • The Testing Ground: To simulate the Unicorn, an Observable that completes swiftly - as swiftly as it does nothing.

Handling a tough crowd - responses

While we work under the dynamic cupola of asynchronous operations, correct handling of the "maybe you have content, maybe not" scenario is crucial:

function errandsToRun(condition) { if (condition) { // There's work to be done, go fetch the data! return offToMarketWeGo(); } else { // Kick back, chillax, here's an EMPTY Observable return silentButDeadly$; } }

This setup allows the subscriber of errandsToRun to always expect an Observable, thus handling completion in style!

Overachievers: Observable with immediate completion

There's another kind of party-goer in town - of({}). It offers more than our friend EMPTY - an empty object, and completion:

import { of } from 'rxjs'; // Not just a pretty face, I carry an (empty) object with me const dressedToImpress$ = of({});

The grand EMPTY vs of({}) debate

Choosing between silentButDeadly$ and dressedToImpress$ boils down to whether your subscriber needs eye-candy:

  • EMPTY is for delving into monk-like asceticism with no values attached.
  • of({}) is for those craving for an appearance, though devoid of any parser-pleasing information.

Strongest there is - TypeScript and generics

Flex your TypeScript muscles by using generics to keep your empty Observables strongly typed:

import { EMPTY } from 'rxjs'; // A burly, typed EMPTY Observable const burly$: Observable<string> = EMPTY;

By specifying the type, TypeScript can enforce that your Observables are brawny and robust!

Staying fit with RxJS6

With RxJS6, you get a fresh new Observable creation methods gym. Stick to the new equipment - using module rxjs/observable is past its prime. The up-and-coming direct rxjs import way, is the path to gains.

The edge: Staying up-to-date with RxJS

Ensure your code gym is not using deprecated equipment. Checking in with the official documentation and updates in RxJS periodically ensures continued strength gains. Stay swole!