javascripttypescriptasync-awaitpromisein-app-purchase

Type 'boolean' is not assignable to type 'Promise<boolean>'


I'm using inApp purchase and I want to change the UI to 'unavailable' if for some reason I'm unable to retrieve the product information from Google or iOS app store.

  ionViewDidEnter() {
    this.platform.ready().then(async () => {
      firebase.auth().onAuthStateChanged(async user => {
        this.currentUser = user;
      });
      this.unavailable = await this.setupProducts();
      console.log('available', this.unavailable);
    });
  }

  setupProducts(): Promise<boolean> {
    let productWWHS: string;
    let productISA: string;

    if (this.platform.is('ios')) {
      productWWHS = 'prodID';
      productISA = 'prodID';

    } else if (this.platform.is('android')) {
      productWWHS = 'prodID';
      productISA = 'prodID';
    }

    this.inAppPurchase.ready(() => {
      this.products.push(this.inAppPurchase.get(productWWHS));
      this.products.push(this.inAppPurchase.get(productISA));
      if (!this.products[0]) {
        return true;
      }
    });
    return false;
  }

I'm doing something wrong in this method, it has the error Type 'boolean' is not assignable to type 'Promise'

I'd like to somehow assert that inAppPurchase.get() has returned something but it doesn't return a promise.

Is there a better way to do this?

Any help would be appreciated.


Solution

  • To fix typing error you need to define the function as async:

    async setupProducts(): Promise<boolean> {
    ...
    return false;
    }
    

    Note that true value from this.inAppPurchase.ready(() => {...}) will not be returned from setupProducts(). It will be returned from anonymous function () => {...} and won't affect anything.

    You probably need something like

    async setupProducts(): Promise<boolean> {
    ...
    await this.inAppPurchase;
    
    this.products.push(this.inAppPurchase.get(productWWHS));
    this.products.push(this.inAppPurchase.get(productISA));
    
    if (!this.products[0]) {
      return true;
    }
    
    return false;
    }
    

    Don't forget () if this.inAppPurchase is a function and not a getter.