【发布时间】:2019-04-18 05:14:32
【问题描述】:
下面是我必须异步执行的函数的一部分。为什么在注释的地方它是未定义的,因为函数返回一个值。如果我的代码不正确,我可以看看它的正确外观吗?
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 返回一个数字,所以我不知道发生了什么。
【问题讨论】:
-
由于您使用的是
await,因此您不应该使用then。 -
请编辑您的示例代码,使其足以说明您遇到的问题。谢谢。
标签: javascript node.js asynchronous sequelize.js