我正在努力通过以下语法找出问题所在:
export const sendVerificationEmail = async () => (dispatch) => { try { dispatch({ type: EMAIL_FETCHING, payload: true }); await Auth.sendEmailVerification(); dispatch({ type: EMAIL_FETCHING, payload: false })) } catch (error) { dispatch({ type: EMAIL_FETCHING, payload: false }); throw new Error(error); } };
我不断收到错误消息:
等待是保留字
…但是在异步函数中不合法吗?
为了使用await,直接封装它的函数需要异步。根据您的评论,添加async到内部函数可以解决您的问题,因此我将其发布在这里:
await
async
export const sendVerificationEmail = async () => async (dispatch) => { try { dispatch({ type: EMAIL_FETCHING, payload: true }); await Auth.sendEmailVerification(); dispatch({ type: EMAIL_FETCHING, payload: false })) } catch (error) { dispatch({ type: EMAIL_FETCHING, payload: false }); throw new Error(error); } };
可能的话,您可以async从外部函数中删除,因为它不包含任何异步操作,但这取决于该调用者是否sendVerificationEmail期望sendVerificationEmail返回承诺。
sendVerificationEmail