gpt4 book ai didi

javascript - Try/Catch/Finally with ESLint 期望在异步箭头函数结束时返回一个值

转载 作者:行者123 更新时间:2023-11-29 20:30:40 25 4
gpt4 key购买 nike

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

函数(productId:任何): 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();
}
};

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

谢谢

最佳答案

这里的规则是consistent-return .

如果 try block 中的 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。重要的是是为了区别于catch中的return false。)

关于javascript - Try/Catch/Finally with ESLint 期望在异步箭头函数结束时返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58431579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com