5

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.

D.Hodges
  • 1,794
  • 4
  • 24
  • 47

2 Answers2

6

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.

Konstantin
  • 354
  • 2
  • 3
  • how does it being a function and not a getter affect anything? Are you suggesting it may be an async operation and I'll need to wait until it finishes? Your suggestion worked by the way but I'd like to understand the implications of the .get function. – D.Hodges Apr 06 '21 at 17:57
  • 1
    @D.Hodges if it is a function declared like this: `async inAppPurchase(): Promise` Then the line `await this.inAppPurchase;` won't execute `inAppPurchase`. To execute it you'll need to call it using `()`: `await this.inAppPurchase();` – Konstantin Apr 07 '21 at 09:54
0

"Type 'boolean' is not assignable to type 'Promise'", typically occurs when you have a function or code that is expected to return a Promise, but you're returning a plain boolean value instead. This is a common error when working with asynchronous code in TypeScript.

function someAsyncFunction(): Promise<boolean> {
return Promise.resolve(true); // Replace 'true' with your boolean result

}

rajquest
  • 535
  • 1
  • 5
  • 10