Try/Catch/Finnaly with ESLint 预期在异步箭头函数结束时返回一个值

You*_*MEL 3 javascript try-catch eslint arrow-functions

我的代码中有这个 ESLint 错误:

function(productId: any): Promise 预期在异步箭头函数结束时返回一个值

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();
  }
};
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题,而不必禁用 ESLing 规则:)

谢谢

Cer*_*nce 5

这里的规则是一致返回

如果块中的if语句try未完成,您将不会返回任何内容。如果isSubscribed调用是真的,你应该返回一些东西:

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();
  }
};
Run Code Online (Sandbox Code Playgroud)

(当然,用Already subscribed那里最有意义的东西替换。如果你只是想表明交易成功,也许return true。重要的是将它与return false中的区分开来catch。)