下面是我的函数的一部分,必须异步执行。为什么在带注释的地方未定义,因为该函数返回一个值。如果我的代码不正确,我可以看看它的外观如何吗?
async function addAvailableFunds(
recipientAvailableFunds,
amountMoney,
recipientId,
transferCurrencyId,
) {
const convertedAmountMoney = await currencyConversion(
transferCurrencyId,
recipientCurrencyId,
amountMoney,
);
console.log(
'convertedAmountMoney',
convertedAmountMoney,
); // undefined
async function currencyConversion(
transferCurrencyId,
recipientCurrencyId,
amountMoney,
) {
console.log('transferCurrencyId', transferCurrencyId);
console.log('recipientCurrencyId', recipientCurrencyId);
console.log('amountMoney', amountMoney);
await Currency.findOne({
where: {
id: recipientCurrencyId,
},
}).then(async isRecipientCurrencyId => {
if (isRecipientCurrencyId) {
const mainCurrency = isRecipientCurrencyId.main_currency;
const recipientCurrencyExchangeRate =
isRecipientCurrencyId.currency_exchange_rate;
console.log('mainCurrency', mainCurrency);
console.log(
'recipientCurrencyExchangeRate',
recipientCurrencyExchangeRate,
);
await Currency.findOne({
where: {
id: transferCurrencyId,
},
}).then(isTransferCurrencyId => {
if (isTransferCurrencyId) {
const transferCurrencyExchangeRate =
isTransferCurrencyId.currency_exchange_rate;
console.log(
'transferCurrencyExchangeRate',
transferCurrencyExchangeRate,
);
if (mainCurrency) {
const convertedAmountMoney =
(amountMoney / transferCurrencyExchangeRate) *
recipientCurrencyExchangeRate;
console.log('convertedAmountMoney', convertedAmountMoney);
return convertedAmountMoney; // return number
}
}
});
}
});
}
console.log返回一个数字,所以我不知道发生了什么。 console.log返回一个数字,所以我不知道发生了什么。
答案 0 :(得分:1)
您正在混合Promise then
模式和async/await
模式。
这是两种不同且不兼容的编码模式。 await
返回一个非Promise值(仅在async
函数的上下文中),但是then
从不返回除其他Promise之外的任何内容。
使用async/await
或Promises,但不能同时使用同一逻辑。
答案 1 :(得分:0)
在currencyConversion
中,您正在混合两种方法来处理返回诺言的函数。
您这样做:
await Currency.findOne(...params..).then(...params..);
尽管您希望在使用async/await
语法时执行以下操作:
let isRecipientCurrencyId = await Currency.findOne(...params..);
...rest of the code..
答案 2 :(得分:0)
convertedAmountMoney
未定义,因为在currencyConversion
中未返回任何内容。您在其他承诺中的.then
内部返回,但是currencyConversion
本身未返回任何内容。
我已经在下面固定了您的代码,以使其完整async/await
,但是您必须处理三个else
,因为到目前为止,您还没有弄清楚什么然后去做。我为此添加了三个警告。
async function addAvailableFunds(
recipientAvailableFunds,
amountMoney,
recipientId,
transferCurrencyId,
) {
const convertedAmountMoney = await currencyConversion(
transferCurrencyId,
recipientCurrencyId,
amountMoney,
);
console.log(
'convertedAmountMoney',
convertedAmountMoney,
); // undefined
}
async function currencyConversion(
transferCurrencyId,
recipientCurrencyId,
amountMoney,
) {
console.log('transferCurrencyId', transferCurrencyId);
console.log('recipientCurrencyId', recipientCurrencyId);
console.log('amountMoney', amountMoney);
const isRecipientCurrencyId = await Currency.findOne({
where: {
id: recipientCurrencyId,
},
})
if (isRecipientCurrencyId) {
const mainCurrency = isRecipientCurrencyId.main_currency;
const recipientCurrencyExchangeRate =
isRecipientCurrencyId.currency_exchange_rate;
console.log('mainCurrency', mainCurrency);
console.log(
'recipientCurrencyExchangeRate',
recipientCurrencyExchangeRate,
);
const isTransferCurrencyId = await Currency.findOne({
where: {
id: transferCurrencyId,
},
})
if (isTransferCurrencyId) {
const transferCurrencyExchangeRate =
isTransferCurrencyId.currency_exchange_rate;
console.log(
'transferCurrencyExchangeRate',
transferCurrencyExchangeRate,
);
if (mainCurrency) {
const convertedAmountMoney =
(amountMoney / transferCurrencyExchangeRate) *
recipientCurrencyExchangeRate;
console.log('convertedAmountMoney', convertedAmountMoney);
return convertedAmountMoney; // return number
}
console.warn('Will return undefined');
}
console.warn('Will return undefined');
}
console.warn('Will return undefined');
}