【问题标题】:async/await function return undefined?异步/等待函数返回未定义?
【发布时间】: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


【解决方案1】:

您正在将 Promise then 模式与 async/await 模式混合。

这是两种不同且不兼容的编码模式。 await 返回一个非 Promise 值(仅在 async 函数的上下文中),但 then 从不返回除另一个 Promise 之外的任何值。

使用 async/await 或 Promises,但不能同时使用相同的逻辑。

【讨论】:

    【解决方案2】:

    在您的 currencyConversion 中,您正在混合使用两种方法来处理返回承诺的函数。

    你这样做:

    await Currency.findOne(...params..).then(...params..);

    虽然您希望在使用async/await 语法时执行以下操作:

    let isRecipientCurrencyId = await Currency.findOne(...params..);
    ...rest of the code..
    
    

    async function

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-25
        • 1970-01-01
        • 2022-11-06
        • 2018-05-27
        • 2011-03-04
        • 2021-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多