【发布时间】:2020-07-02 06:59:23
【问题描述】:
我的减速器无法返回正确的状态。从我的其他组件访问天气时,我得到了一个承诺。我在 getWeather 函数中使用了 async await,但我不确定为什么我仍然从返回值中得到一个 Promise。
const initState = {
date: new Date(),
weather: "",
};
const getWeather = async (difference) => {
await fetch(
"https://api.openweathermap.org/data/2.5/onecall?lat=1.290270&lon=103.851959&%20exclude=hourly,daily&appid=" +
WEATHER_API_KEY
)
.then((response) => response.json())
.then((data) => {
return data["daily"][difference]["weather"][0]["main"];
});
};
export default function (state = initState, action) {
switch (action.type) {
case DATE_SELECT:
const day_difference =
moment(action.payload).date() - moment(state.date).date();
return {
date: action.payload,
weather: getWeather(day_difference),
};
default:
return state;
}
}
【问题讨论】:
标签: reactjs redux async-await