【发布时间】:2019-11-12 19:02:47
【问题描述】:
我在 javascript 中的函数内部有一个函数。我在最里面的函数中得到了我的值,但我需要在最外面的函数中返回它。像这样:
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
};
【问题讨论】:
标签: javascript reactjs ecmascript-6 axios