-1

I have a function inside a function inside a function in javascript. I get my value in the inner most function, but I need to return it in the outer most one. Like this:

import convertPrice from "./convertPrice";
import getCountryInfoByLanguage from "./getCountryInfoByLanguage";
import axios from "axios";
export default (language, price) => {
  let countryCode, currency, formattedPrice;

  navigator.geolocation.getCurrentPosition(position => {
    axios
      .get(
        `http://api.geonames.org/countryCodeJSON?lat=${
          position.coords.latitude
        }&lng=${position.coords.longitude}&username=...`
      )
      .then(response => {
        countryCode = getCountryInfoByLanguage(
          response.data.languages.split(",")[0]
        ).countryCode;
        currency = getCountryInfoByLanguage(
          response.data.languages.split(",")[0]
        ).currency;
        const convertedPrice =
          Math.round(convertPrice(price, currency) * 10) / 10;
        formattedPrice = convertedPrice.toLocaleString(countryCode, {
          style: "currency",
          currency: currency
        });
        console.log(formattedPrice);
        return formattedPrice; //This is where the value gets properly returned
      });

  });
    //this is where I want the value to be returned
};

Filip
  • 855
  • 2
  • 18
  • 38
  • 3
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ASDFGerte Nov 12 '19 at 19:05

1 Answers1

1

Since you are dealing with promises, you can always returns them and call a final then at the end.

import convertPrice from "./convertPrice";
import getCountryInfoByLanguage from "./getCountryInfoByLanguage";
import axios from "axios";
export default (language, price) => {
  let countryCode, currency, formattedPrice;

  navigator.geolocation.getCurrentPosition(position => {
    return axios
      .get(
        `http://api.geonames.org/countryCodeJSON?lat=${
          position.coords.latitude
        }&lng=${position.coords.longitude}&username=filipk268`
      )
      .then(response => {
        countryCode = getCountryInfoByLanguage(
          response.data.languages.split(",")[0]
        ).countryCode;
        currency = getCountryInfoByLanguage(
          response.data.languages.split(",")[0]
        ).currency;
        const convertedPrice =
          Math.round(convertPrice(price, currency) * 10) / 10;
        formattedPrice = convertedPrice.toLocaleString(countryCode, {
          style: "currency",
          currency: currency
        });
        console.log(formattedPrice);
        return formattedPrice; //This is where the value gets properly returned
      });

  }).then((data) => {
      console.log(data);
  })
};

Notice here that i'm returning the axios call, which returns a promise, You could even return the navigator.geolocation.getCurrentPosition it would be accessible as a promise outside the current scope.

Nicolas
  • 8,077
  • 4
  • 21
  • 51