BACK
Lessons5. The of creation operator
Lessons · 05

5. The of creation operator

The simplest way to create a stream

In real life we rarely write new Observable by hand. RxJS ships ready-made "factory" functions called creation operators. The simplest of them is of.

How of works

of(a, b, c, ...) takes its values as separate arguments. Once you subscribe, it emits them one by one synchronously (right away, with no delay) and then calls complete.

of(10, 20, 30)
// Signal sequence: next(10) → next(20) → next(30) → complete()

Where it's handy

Most often of is used for tests, stubbed data, and for returning a ready value from a function that must return an Observable (for example inside catchError, which we'll cover later).

Your task

  1. Pass three numbers into of(...): 10, 20, 30 (comma-separated, as separate arguments).
  2. The subscription is passed as an object — it already logs each number via next and a Done message via complete.
  3. Expected output: Number: 10 → Number: 20 → Number: 30 → Done.
Solution spoiler · click to reveal
const { of } = Rx;

const numbers$ = of(10, 20, 30);

numbers$.subscribe({
  next: value => console.log('Number: ' + value),
  complete: () => console.log('Done')
});
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...