Creating a Stream from an Array of Items Using the RxJS From Operator

Laurie Barth
InstructorLaurie Barth

Share this video with your friends

Send Tweet
Published 3 years ago
Updated 2 years ago

Use the RxJS from operator to create a stream with an array of data. Use the interval and zip operators to time delay each element in the stream.

Instructor: [00:01] Begin by importing the from operator from RxJS. Next, define an observable using the from operator, passing in an array of numbers. In order to use the observable, we'll subscribe to it and pass in an observer that logs out the value.

[00:27] As we can see, all of the numbers appear at once. We'll use the interval operator to define an observable that has a time series.

[00:40] Finally, we'll use the zip operator to define another observable that takes in our from observable and our timed interval observable. We'll subscribe to our zipped observable. As we can see, we're logging an array.

[01:03] We'll use destructuring assignment to access the first element in that array, the returned number from our from observable.

[01:14] There we have it, each element in our array appearing in a time-delayed stream.