RxJs Create Observable from resulting Promise RxJs Create Observable from resulting Promise angularjs angularjs

RxJs Create Observable from resulting Promise


Use Rx.Observable.fromPromise(promise)

fromPromise:

Converts a Promises/A+ spec compliant Promise and/or ES2015 compliant Promise or a factory function which returns said Promise to an Observable sequence.

example:

var source = Rx.Observable.fromPromise(promise);var subscription = source.subscribe(  function (x) {    console.log('Next: %s', x);  },  function (err) {    console.log('Error: %s', err);  },  function () {    console.log('Completed');  });

update

rxjs6 method is from


update

As of rxjs6 you can use from()


Did you tried to use the fromPromise() API of rxjs5 ?

Check it's documentation here !


I found the answer here (Just slightly differently named)rxjs using promise only once on subscribe

So for my example the answer is as simple as:

var loadCountries = function () { return $http.get('/countries'); };var observable = Rx.Observable.defer(loadCountries).shareReplay();