1

Below is part of my function that must be performed asynchronously. why in a commented place it is undefined, since the function returns a value. And if my code is incorrect, can I see how it should look correctly?

    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 returns a number so I do not know what's going on. console.log returns a number so I do not know what's going on.

2
  • 4
    Since you're using await, you should not use then. Commented Apr 17, 2019 at 20:46
  • Please edit your example code to just enough to illustrate the issue you are having. Thanks. Commented Apr 17, 2019 at 20:51

3 Answers 3

1

You are mixing the Promise then pattern with that of async/await.

These are two different and incompatible coding patterns. await returns a non-Promise value (only within the context of an async function), but then never returns anything other than another Promise.

Either use async/await or Promises, but not both in the same logic.

Sign up to request clarification or add additional context in comments.

Comments

0

Inside of your currencyConversion you are mixing two approaches to handle functions that return promises.

You do:

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

Although you would like to do the following as you are using async/await syntax:

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

async function

Comments

0

convertedAmountMoney was undefined because nothing was returned in currencyConversion. You returned inside a .then inside some other promise, but currencyConversion itself returned nothing.

I've fixed your code below so as to go fully async/await, but there are three elses you'll have to handle yourself, because as of now your you didn't precised what to do then. I added three warnings for this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.