Try/Catch/Finnaly with ESLint Expected to return 异步箭头函数末尾的值
Try/Catch/Finnaly with ESLint Expected to return a value at the end of async arrow function
我的代码中有这个 ESLint 错误:
function(productId: any): 承诺
预期 return 异步箭头函数末尾的值
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
块中的 if
语句未实现,则您没有 returning 任何内容。如果 isSubscribed
调用是真实的,你应该 return 一些东西:
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
.)
我的代码中有这个 ESLint 错误:
function(productId: any): 承诺 预期 return 异步箭头函数末尾的值
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
块中的 if
语句未实现,则您没有 returning 任何内容。如果 isSubscribed
调用是真实的,你应该 return 一些东西:
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
.)