1

At some point in the code, if a condition is true, I need to use data from external API, which is fetched asynchronously like this:

# ./helper/currencyConverter.js

const axios = require('axios').default;

const convert = async (curr, amount) => {
  try {
    const response = await axios.get('https://api.example.com/endpoint');
    const data = response.data;

    // do conversion

    return convertedAmount;

  } catch (error) {
    throw new Error(error);
  }
}

module.exports = { convert }

The module above is called from a non-async function if certain condition is met, then return data processed by the async function, otherwise return unprocessed data, as demonstrated here:

# ./models/calculator.js
const currencyConverter = require('../helpers/currencyConverter');

const calculate = data => {

  const amount = data.amount;

  if (true) {
    currencyConverter.convertToEur(currency, amount).then(convertedAmount => {
      return convertedAmount;
    });
  }

  return amount; 
}

The problem is that return amount always returns earlier than return convertedAmount. How do I make the return convertedAmount to return if the condition is true?

qwaz
  • 1,285
  • 4
  • 23
  • 47
  • 2
    You cannot call an asynchronous function from a synchronous function, at least not if you want the return values. This is because you may have to wait for the return value to be computed (that's what "asynchronous" means). Therefore `calculate` must also be an asynchronous function in which you `await currencyConverter.covertToEur(...)`. – Heiko Theißen Aug 12 '22 at 14:34

1 Answers1

1

You can just return a promise and resolve it when you execute the calculate function.

const calculate = data => {
  const amount = data.amount;
  let condition = true
  return condition ? 
    currencyConverter.convertToEur(currency, amount) : Promise.resolve(amount)
}

// resolve the promise returned by calculate function.
calculate(data).then(res => console.log(res))

Or async/await syntax to stop return the function until it is processed by the convertToEur function.

const calculate = async data => {
  const amount = data.amount;

  if (true) {
    amount = await currencyConverter.convertToEur(currency, amount)
  }

  return amount; 
}
Mina
  • 14,386
  • 3
  • 13
  • 26