Skip to main content

What does from() do in RxJS?

from() in RxJS is a function that turns something "ordinary" into a stream (Observable).


What you can pass to from():

  1. An array
ts
from([1, 2, 3]).subscribe(x => console.log(x)); // Outputs: 1, 2, 3 (one value at a time)
  1. A promise
ts
from(fetch('https://api.example.com')).subscribe(response => ...); // Fires when the promise resolves
  1. A string, Map, Set, Iterable
ts
from('abc').subscribe(x => console.log(x)); // Outputs: a, b, c

Why you need it:

Angular often works with Observable, but you might have a plain array or a promise, from() turns them into a reactive stream.


Conclusion:

from() is a way to take "ordinary" data and start working with it reactively. It turns an array, string, or promise into an Observable. From there you can apply .pipe(), map, filter, subscribe, and everything else.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.