Reputation: 6441
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.
Upvotes: 36
Views: 25278
Reputation: 75
Promise.resolve(), if given a promise, will return an identical promise. It’s essentially a no-op, but it’s a useful way to ensure that whatever “thing” you have is promise-wrapped.
Observable.of(), by contrast, if given an observable, is not a no-op; it will return an observable that wraps the original observable.
For of() to be comparable to this aspect of Promise.resolve, the output here:
rxjs.of( rxjs.of(1,2,3) ).subscribe( { next: v => console.log(v) } )
… should be 1, 2, 3, but instead it’s 3 observables.
Unfortunately, I don’t know of an RxJS operator that can do this.
Upvotes: 0
Reputation: 48525
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);
}
Upvotes: 34