尝试/捕获/最终使用ESLint预期在异步箭头功能的末尾返回一个值

问题描述 投票:0回答:1

我的代码中出现此ESLint错误:

function(productId:any):承诺预期在异步箭头功能的末尾返回一个值]

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();
  }
};

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

感谢

javascript try-catch eslint arrow-functions
1个回答
0
投票
如果未满足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(); } };

((当然,将Already subscribed替换为最有意义的内容。如果您只是想表明交易成功,也许是return true。重要的是将其与return false中的catch区别开]。)
© www.soinside.com 2019 - 2024. All rights reserved.