簡體   English   中英

異步/等待函數返回未定義?

[英]async/await function return undefined?

下面是我必須異步執行的函數的一部分。 為什么在注釋的地方它是未定義的,因為該函數返回一個值。 如果我的代碼不正確,我能看到它應該如何正確顯示嗎?

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

您將 Promise then模式與async/await模式混合在一起。

這是兩種不同且不兼容的編碼模式。 await返回一個非承諾值(僅適用於內的情況下async函數),但是then不會返回比其他承諾的任何其他。

使用async/await或 Promises,但不能在同一邏輯中使用。

在您的currencyConversion您混合了兩種方法來處理返回承諾的函數。

你做:

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

盡管您在使用async/await語法時想要執行以下操作:

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

異步函數

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM