Home » RxJS from() Creation Operator

RxJS from() Creation Operator

by Online Tutorials Library

RxJS from() Creation Operator

RxJS from() operator is a creation operator used to create an observable from an array, an array-like object, a promise, an iterable object, or an observable-like object.

In other words, we can say that the RxJS from() operator is used to transform data that can be iterated over to an observable. It turns an array, promise, or iterable into an observable.

It is most useful when we have to normalize the types of data that is being passed and shared in observable sequences or when a function expects to receive and act on an observable. It is also preferred to use when we want to use an RxJS operator that wouldn’t normally be available on the original data type.

It converts almost everything to an Observable.

Syntax:

Following is the syntax of the RxJS from() creation operator:

Or

Parameter Explanation

  • input: It specifies the input given to this operator. It is observable. Type: any.
  • scheduler: It is an optional argument. Its default value is undefined. Type: SchedulerLike.

Return value

The RxJS from() operator returns an observable.

Let us see some examples of the RxJS from() operator to understand it clearly.

Example 1 (Observable from array)

Output:

After executing the above example, you will see the following result:

RxJS from() Creation Operator

Example 2 (Observable from promise)

Output:

After executing the above example, you will see the following result:

RxJS from() Creation Operator

Example 3 [Convert an infinite iterable (from a generator) to an observable]

Output:

After executing the above example, you will see the following result:

RxJS from() Creation Operator


Next TopicRxJS Operators

You may also like