【问题标题】:Try/Catch/Finnaly with ESLint Expected to return a value at the end of async arrow function使用 ESLint 的 Try/Catch/Finally 期望在异步箭头函数的末尾返回一个值
【发布时间】:2019-10-17 11:28:37
【问题描述】:

我的代码中有这个 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 规则 :)

谢谢

【问题讨论】:

  • 在您的 try 块中,您在 if 中有返回类型,但是在外面呢?如果你的代码执行没有进入 if,try 块应该返回一些东西
  • 我的回答解决了你的问题吗?

标签: javascript try-catch eslint arrow-functions


【解决方案1】:

这里的规则是consistent-return

如果try 块中的if 语句未完成,则您不会返回任何内容。如果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区分开来987654329@.)

【讨论】:

    猜你喜欢
    • 2020-05-06
    • 2021-11-09
    • 2022-01-20
    • 2021-01-15
    • 2021-11-22
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多