Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS alternative to doing a Promise.resolve?

Tags:

angular

rxjs

What is the equivalent in RxJS to Promise.resolve? I know I can do Observable.fromPromise(Promise.resolve(someValue)); but there has to be a cleaner way.

like image 487
Davy Avatar asked Jan 05 '16 16:01

Davy


People also ask

Is Observable better than Promise?

The biggest difference is that Promises won't change their value once they have been fulfilled. They can only emit (reject, resolve) a single value. On the other hand, observables can emit multiple results. The subscriber will be receiving results until the observer is completed or unsubscribed from.

What is equivalent to switchMap in promises?

switchMap is one of the most useful RxJS operators because it can compose Observables from an initial value that is unknown or that change. Conceptually, it is similar to chaining then functions with Promises, but operates on streams (Promises resolve once).

What is difference between RxJS and Promise?

the Promise can provide a single value, whereas the Observable is a stream of values (from 0 to multiple values), you can apply RxJS operators to the Observable to get a new tailored stream.

Why does angular use observables instead of promises?

Often Observable is preferred over Promise because it provides the features of Promise and more. With Observable it doesn't matter if you want to handle 0, 1, or multiple events. You can utilize the same API in each case. Observable also has the advantage over Promise to be cancellable.


1 Answers

Observable.of is what you are looking for (see this plunk):

// You might need to add this import in RxJS versions earlier than 5 import 'rxjs/add/observable/fromArray';  // ... or this line in RxJS 5+ import 'rxjs/add/observable/of';  if (me.groups) {   return Observable.of(me.groups); } 
like image 175
alexpods Avatar answered Oct 04 '22 11:10

alexpods