I'm trying to wire redux-thunk into Next.js and it works fine if my thunk returns a promise. How do I convert that to use async/await? I took a look at this article (how to async/await redux-thunk actions?) but I'm having trouble wrapping my head around it.
My getInitialProps
in my index.js is
static getInitialProps(props) {
const {store, isServer} = props.ctx;
if (!store.getState().placeholderData) {
return store.dispatch(loadData());
}
and currently my action that handles loading the data is
export const loadData = () => (dispatch) => {
return isoFetch(homeES_endpoint)
.then(res => res.json())
.then(data => dispatch(loadDataSuccess(data)))
.catch(err => console.error('error loading data', err.toString()));
}
How might I convert loadData to use async/await? I've tried
export const loadData = () => async (dispatch) => {
try {
const res = await isoFetch(homeES_endpoint);
const data = await res.json();
dispatch(loadDataSuccess(data));
} catch(error) {
//dispatch({ type: LOGIN_ERROR, error });
console.error('error loading data',error.toString())
}
}
but the main getInitialProps
in '_app.js' doesn't wait for it.