Спасибо за ответ, Кирилл,
Я пытался реализовать ваш код, но получил
ReferenceError: Cannot access 'promises' before initialization
getCityWeather
src/containers/WeatherData/WeatherData.js:39
36 | `${baseUrl}/weather?q=${city}&units=metric&appid=${appiID}`,
37 | `${baseUrl}/forecast?q=${city}&units=metric&appid=${appiID}`
38 | ];
> 39 | const promises = urls.map(s => axios.get(s),
| ^ 40 |
41 | Promise.all(promises)
42 | .then(([weatherResponse, forecastResponse]) => {
Ниже, пожалуйста, найдите мою реализацию для компонента на основе классов, который я использую
getCityWeather = (event) => {
event.preventDefault();
const baseUrl = 'https://api.openweathermap.org/data/2.5';
const { city, appiID } = this.state;
const urls = [
`${baseUrl}/weather?q=${city}&units=metric&appid=${appiID}`,
`${baseUrl}/forecast?q=${city}&units=metric&appid=${appiID}`
];
const promises = urls.map(s => axios.get(s));
Promise.all(promises)
.then(([weatherResponse, forecastResponse]) => {
const {
date: {
weather,
name,
sys: { country },
main,
wind,
clouds
},
} = weatherResponse;
const {
data: { list },
} = forecastResponse;
this.setState({
currentCityWeather: weather,
currentCityName: name,
currentCityCountry: country,
CityWeatherMain: main,
CityWeatherWind: wind,
CityWeatherClound: clouds,
CityForecastDataList: list,
cityLoaded: true,
});
})
.catch(({ message }) => {
console.error(message);
})
)}