Try/Catch/Finnaly with ESLint Expected to return a value at the end of async arrow function

Viewed 5112

I have this ESLint error in my code:

function(productId: any): Promise Expected to return a value at the end of async arrow function

export const getGooglePlayPayment = async (productId) => {
  await InAppBilling.close();
  try {
    await InAppBilling.open();

    if (!await InAppBilling.isSubscribed(productId)) {
      const details = await InAppBilling.subscribe(productId);
      console.log('You purchased: ', details);
      return details.purchaseState === PAYMENT_STATE.PurchasedSuccessfully;
    }
  } catch (err) {
    console.log(err);
    return false;
  } finally {
    await InAppBilling.consumePurchase(productId);
    await InAppBilling.close();
  }
};

Someone can help me to fix this issue without having to disabled the ESLing rule :)

thanks

1 Answers

The rule here is consistent-return.

You are not returning anything if the if statement in the try block is not fulfilled. You should return something if the isSubscribed call is truthy:

export const getGooglePlayPayment = async (productId) => {
  await InAppBilling.close();
  try {
    await InAppBilling.open();

    if (!await InAppBilling.isSubscribed(productId)) {
      const details = await InAppBilling.subscribe(productId);
      console.log('You purchased: ', details);
      return details.purchaseState === PAYMENT_STATE.PurchasedSuccessfully;
    }
    return 'Already subscribed';
  } catch (err) {
    console.log(err);
    return false;
  } finally {
    await InAppBilling.consumePurchase(productId);
    await InAppBilling.close();
  }
};

(Of course, replace Already subscribed with whatever would make most sense there. If you just want to indicate that the transaction succeeded, perhaps return true. The important thing is to distinguish it from the return false in the catch.)

Related