fromPromise does not exist on type Observable
Asked Answered
S

2

102

In Angular 2 using rxjs I was trying to convert a Promise to Observable. As many of online guides showed I used fromPromise on Observable. Which throws error:

Property 'fromPromise' does not exist on type 'typeof Observable'.

Observable was imported like:

import { Observable } from "rxjs/Observable";

trying to import fromPromise like other operators results in error:

import 'rxjs/add/operator/fromPromise';

even if I suppress typescript error it still results in error:

(<any>Observable).fromPromise

Error:

Uncaught (in promise): TypeError: __WEBPACK_IMPORTED_MODULE_3_rxjs_Observable__.Observable.fromPromise is not a function

Somewhat similar issue was reported on rxjs repo here but there is no solution there either.

Swingle answered 20/8, 2017 at 17:16 Comment(4)
pleas check the version of typescript and rxjs you are using, Observable.fromPromise just should work. check for the file fromPrommis.js in rxjs -> add -> observable -> fromPromise.jsIncompliant
rxjs 5.4.0 typescript 2.3.4Swingle
and the fromePromise.js does existSwingle
That explains. Check out the latest version and it should contain that file. If you r on latest then just remove your node modules and do npm i.Incompliant
S
215

UPDATE:

As of rxjs 6.0.0-beta.3, operators and observable creators should be imported from rxjs. Furthermore, fromPromise is not part of the public API anymore and its wrapped in the from method.

TL;DR;

UPDATE

For rxjs 6.0.0 use:

import { from } from 'rxjs';

var observableFromPromise =  from(promiseSrc);

UPDATE:

After the release of the pipeable operators in rxjs 5.5.x, the monkey patch approach is strongly discouraged. Consider to use the static method option.

Original answer

As of rxjs 5.4.x, fromPromise can be used as a static method or can be patched into the Observable prototype.

For the first, you can do the following:

import { fromPromise } from 'rxjs/observable/fromPromise';

var observableFromPromise = fromPromise(promiseSrc);

More info about this approach here

To do the second, you need to change your import statement:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';

var observableFromPromise = Observable.fromPromise(promiseSrc);

More info about this approach here

Personally I would recommend the first one, considering that the 2nd approach is basically the 1rst, with the difference that the Observable prototype is changed.

Shuntwound answered 20/8, 2017 at 18:26 Comment(0)
B
9

like what Jota said 'from' is the answer.

you can find the reference from here

https://www.learnrxjs.io/operators/creation/from.html

However, if you want to specify 'Promise to Observable' you could use 'fromPromise' like below.

  import { from as fromPromise, Observable} from 'rxjs';
  ...

  private getObservable(): Observable<any> {
    return fromPromise(this.promise);
  }


  private getPromise() {

   this.promise = new Promise((resolve, reject) => {
      this.service.getPromise()
        .then(response => {
          //  do sth
          resolve(response);
        });
    });
}
Bairam answered 17/2, 2019 at 3:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.