Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS: Observable.create() vs. Observable.from()

What's the difference between these two?

return Observable.create(function(observer) {
    if (array)
        observer.next([]);
    else
        observer.next(null);
    observer.complete();
});

and

return Observable.from( array ? [] : null );

I thought it could be the same but didn't work the same.

like image 902
Ondra Žižka Avatar asked Dec 13 '16 01:12

Ondra Žižka


People also ask

How do I create an observable in RxJS?

To create an Observable, you have to first import Observable from RxJS in the.ts file of the component you want to create it in. The creation syntax looks something like this: import { Observable } from "rxjs"; var observable = Observable.create((observer:any) => { observer.next('Hello World!')

What is an observable in JavaScript?

An Observable is a lazily evaluated computation that can synchronously or asynchronously return zero to (potentially) infinite values from the time it's invoked onwards. For more info about what to use when converting Observables to Promises, please refer to this guide. Observables as generalizations of functions

What is the difference between observable create and observable constructor?

Create is a method of the observable object, Hence you do not have to import it. We looked at this in the previous tutorial. There is no difference between the Observable.create method and observable constructor.

What is observable from from in RX?

Observable.from(): You have a list of values (that has nothing to do with Rx) and an Observable is created from this list. When an Observer subscribe to this Observable, the list of values are passed as Next() events one by one then Completed() is eventually passed and you cannot control this process.


1 Answers

The create(...) is a generic Observable factory method for creating an Observable in which you will explicitly dictate how values are passed to the Subscriber

For instance, if you were to create a timer base Observable (don't it already exists as Observable.timer) you could do:

   Observable.create(observer => {
     const timeoutId = setTimeout(() => {
       observer.next(0);
       observer.complete();
     }, 500);

     return () => clearTimeout(timeoutId);
   });

The from(...) is what I call a conformance operator in that it attempts to coerce a passed in data type into an Observable (make it conform). This means that it will accept a variety of types and convert them into Observables. These types include:

  • Arrays
  • Promises
  • Generators
  • Observable-like things

There are specific converters as well that you can find such as fromArray and fromPromise which specifically convert those types, but from more of a swiss-army knife of those methods

If you just need a single value you should be using Observable.of (the docs appear to be out of date, just/return was renamed to of in RxJS 5 and I don't think they are aliased anymore).

i.e.

// Don't quote me on the import part
import 'rxjs/add/observable/of';

Observable.of(1, 2, 3, 4).subscribe();
like image 96
paulpdaniels Avatar answered Sep 27 '22 23:09

paulpdaniels