Как вернуть Observable из Promise - PullRequest
       20

Как вернуть Observable из Promise

0 голосов
/ 24 апреля 2018

Мне нужно вернуть Observable из обещания, однако я застрял в синтаксисе, пытаясь выучить angular. Итак, вот что:

У меня есть следующая функция getCountries (на которую подписываетсядругая утилита), где мне нужно позвонить, чтобы получить список стран. «Язык» еще не определен. Я добавил, что просто для завершения URL моего API.

getCountries(): Observable<Country[]> {
    if (this.countries) {
       return Observable.of(this.countries);
    } else if (this.countriesObservable) {
      return this.countriesObservable;
    } else {
//lang is not defined yet it has to be retrieved from storage

      this.countriesObservable = this.http.get(AppSettings.API_URL + '?locale=' + lang).map(json => {
        delete this.countriesObservable; // when the cached countries is available we don't need  reference anymore
        this.countries = json as Country[];
        this.countries = this.countries.sort(function (a, b) { return (a.Name > b.Name) ? 1 : ((b.Name > a.Name) ? -1 : 0); });
        return this.countries;
      }).share();
      return this.countriesObservable;
    }
  }

Поскольку для API требуется язык (lang), и эта информация хранится в хранилище, которое можно извлечь из хранилища следующим образом:

  this.storage.get(AppSettings.STORAGE_KEYS.LANGUAGE_APP).then(language=>{
        //here I can use language
      });

Теперь место, где я застрял, - какпозвонить в мой API после получения информации о языке из приведенного выше обещания.Мне нужно убедиться, что функция (getCountries) по-прежнему возвращает наблюдаемое, которое она возвращала изначально.Спасибо за вашу помощь.

Некоторые люди предложили обернуть их в Observable.fromPromise, я сделал то же самое, однако это сломало код. Я, должно быть, что-то упустил, что я знаю.что-то, публикуя мой след стека после кода:

 getCountries(): Observable<Country[]> {
    if (this.countries) {
       return Observable.of(this.countries);
    } else if (this.countriesObservable) {
      return this.countriesObservable;
    } else {
      Observable.fromPromise(this.storage.get(AppSettings.STORAGE_KEYS.LANGUAGE_APP)).subscribe(language=>{
        //here I can use language
        this.countriesObservable = this.http.get(AppSettings.API_URL + '?locale=' + language).map(json => {
          delete this.countriesObservable; 
          this.countries = json as Country[];
          this.countries = this.countries.sort(function (a, b) { return (a.Name > b.Name) ? 1 : ((b.Name > a.Name) ? -1 : 0); });
          return this.countries;
        }).share();
        return this.countriesObservable;
      });
    }
  }



Error: Uncaught (in promise): TypeError: Cannot read property 'subscribe' of undefined
TypeError: Cannot read property 'subscribe' of undefined
    at http://localhost:8100/build/main.js:71:33
    at new t (http://localhost:8100/build/polyfills.js:3:20886)
    at UtilsProvider.webpackJsonp.10.UtilsProvider.getCountryById (http://localhost:8100/build/main.js:70:16)
    at MyApp.webpackJsonp.851.MyApp.updateUserInfo (http://localhost:8100/build/main.js:10067:47)
    at http://localhost:8100/build/main.js:9774:23
    at SafeSubscriber.schedulerFn [as _next] (http://localhost:8100/build/vendor.js:4004:36)
    at SafeSubscriber.__tryOrUnsub (http://localhost:8100/build/vendor.js:16987:16)
    at SafeSubscriber.next (http://localhost:8100/build/vendor.js:16934:22)
    at Subscriber._next (http://localhost:8100/build/vendor.js:16874:26)
    at Subscriber.next (http://localhost:8100/build/vendor.js:16838:18)
    at c (http://localhost:8100/build/polyfills.js:3:19132)
    at c (http://localhost:8100/build/polyfills.js:3:18841)
    at http://localhost:8100/build/polyfills.js:3:19613
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15040)
    at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4238:33)
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:14961)
    at r.runTask (http://localhost:8100/build/polyfills.js:3:10214)
    at o (http://localhost:8100/build/polyfills.js:3:7274)
    at e.invokeTask [as invoke] (http://localhost:8100/build/polyfills.js:3:16203)
    at p (http://localhost:8100/build/polyfills.js:2:26993)

Ответы [ 2 ]

0 голосов
/ 24 апреля 2018

Просто используйте Observable.fromPromise.Так что в вашем случае это будет

getCountries(): Observable<Country[]> {
    if (this.countries) {
       return Observable.of(this.countries);
    } else if (this.countriesObservable) {
      return this.countriesObservable;
    } else {
      return Observable.fromPromise(this.storage.get(AppSettings.STORAGE_KEYS.LANGUAGE_APP)).mergeMap(language=>{
        //here I can use language
        this.countriesObservable = this.http.get(AppSettings.API_URL + '?locale=' + language).map(json => {
          delete this.countriesObservable; // when the cached countries is available we don't need the  reference anymore
          this.countries = json as Country[];
          this.countries = this.countries.sort(function (a, b) { return (a.Name > b.Name) ? 1 : ((b.Name > a.Name) ? -1 : 0); });
          return this.countries;
        }).share();
        return this.countriesObservable;
      });
    }
  }
0 голосов
/ 24 апреля 2018

Любое Обещание можно (легко) превратить в Наблюдаемое с помощью: Observable.fromPromise()

Observable.fromPromise(this.storage.get(AppSettings.STORAGE_KEYS.LANGUAGE_APP))
.subscribe((language) => {
    //here you can use language
});
...