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():
- An array
ts
from([1, 2, 3]).subscribe(x => console.log(x));
// Outputs: 1, 2, 3 (one value at a time)- A promise
ts
from(fetch('https://api.example.com')).subscribe(response => ...);
// Fires when the promise resolves- A string, Map, Set, Iterable
ts
from('abc').subscribe(x => console.log(x));
// Outputs: a, b, cWhy 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 readyPremium
A concise answer to help you respond confidently on this topic during an interview.