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.
Asked
Active
Viewed 2.4k times
36

Mark Amery
- 143,130
- 81
- 406
- 459

Davy
- 6,295
- 5
- 27
- 38
-
To add-up on alexpods answers, the Rx documentation for [observable](https://github.com/ReactiveX/rxjs/blob/master/doc/observable.md) and [observer](https://github.com/ReactiveX/rxjs/blob/master/doc/observer.md) is really enlightening. – Standaa - Remember Monica Oct 25 '16 at 09:25
2 Answers
34
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);
}

Mark Amery
- 143,130
- 81
- 406
- 459

alexpods
- 47,475
- 10
- 100
- 94
-
What i find incredibly confusing is that there are 2 libraries with the same name: https://github.com/Reactive-Extensions/RxJS and https://github.com/ReactiveX/RxJS The "of" method seems to exist in the Microsoft variant, but not in the other variant. The version i am using (the same one that is used in the quickstart at angular.io) does not have this of operator. – Davy Jan 05 '16 at 17:02
-
1@Davy angular2 uses https://github.com/ReactiveX/RxJS its 5.0 version of rxjs (complete rewrite). As you can find it in the docs "This rewrite is meant to have better performance, better modularity, better debuggable call stacks, while staying mostly backwards compatible, with some breaking changes that reduce the API surface." + they want to follow [ES7 observable spec](https://github.com/zenparsing/es-observable) – alexpods Jan 05 '16 at 17:08
-
Hmm, it should be there. Look at [this source code](https://github.com/ReactiveX/RxJS/blob/676f82d72d2c370b438fbc60caaa604654f97e33/src/add/observable/fromArray.ts#L4) – alexpods Jan 05 '16 at 17:10
-
1
-
1
-
Funny. I just read the docs of fromArray and used Observable.fromArray([me.groups]), which worked. Didnt realize that this also imports the of method. – Davy Jan 05 '16 at 17:17
-
I wonder ... shouldnt the "of" method be defined in a file of its own? Including it in the fromArray file makes it hard to find your way around this framework. Anyway, thanx for the assistance alex! – Davy Jan 05 '16 at 17:19
-
-
BTW Mark is right, don't use `var self = this` pattern` with fat arrow functions. – alexpods Jan 05 '16 at 17:24
-
-
In `rxjs 5.x` you will need to import it like so `import 'rxjs/add/observable/of';` – realappie Mar 03 '17 at 13:52
-
1From Angular 6 import like this: `import { of } from 'rxjs';`. You can read more in this thread: https://stackoverflow.com/questions/36568388/ – Maiko Kingma Sep 11 '18 at 07:04
0
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.

PeregrineYankee
- 75
- 5