异步/等待函数返回未定义的值吗?

时间:2019-04-17 20:44:14

标签: javascript node.js asynchronous sequelize.js

下面是我的函数的一部分,必须异步执行。为什么在带注释的地方未定义,因为该函数返回一个值。如果我的代码不正确,我可以看看它的外观如何吗?

    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返回一个数字,所以我不知道发生了什么。

3 个答案:

答案 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..

async function

答案 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');
}