Youcef EL KAMEL
Youcef EL KAMEL

Reputation: 155

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

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

Upvotes: 5

Views: 8350

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 371098

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.)

Upvotes: 8

Related Questions