我在React中有一个dataService函数来执行我的API提取。我尝试转换为异步/等待块,但似乎遇到了障碍。
使用承诺:
const dataService = (url, options, dataToPost) => {
return (dispatch, getState) => {
const { requestAction, successAction, failureAction } = options.actions;
if (options.shouldRequest(getState())) {
dispatch(requestAction());
const promise = axios.get(url, { withCredentials: true });
return promise
.then(response => {
if (response.status === 200) {
return dispatch(successAction(response, dispatch));
}
return Promise.reject(response);
})
.catch(error => {
if (error.response.status === 302) {
window.location = '/view';
}
dispatch(openErrorDialog());
return dispatch(failureAction(error));
});
}
return Promise.reject(new Error('FETCHING'));
};
};
使用异步/等待:
const dataService = async (url, options, dataToPost) => {
return async (dispatch, getState) => {
let url;
const {requestAction, successAction, failureAction} = options.actions;
if (options.shouldRequest(getState())) {
dispatch(requestAction());
const promise = axios.get(url, {withCredentials: true});
try {
const response = await promise;
if (response.status === 200) {
return dispatch(successAction(response, dispatch));
}
return Promise.reject(response);
} catch (error) {
return dispatch(failureAction(error));
}
}
return Promise.reject(new Error('FETCHING'));
};
};
错误是“操作必须是普通对象。将自定义中间件用于异步操作。”。 Promise代码完美无缺。我已经在使用thunk了。请指教。
答案 0 :(得分:2)
如果您确实要更改Promise-> async / await,则更改如下:
首先,您不要希望dataService为async
,因为这将意味着它返回一个Promise,从而改变了它的调用方式-您不必担心
其次,改变
const promise = axios.get ...
promise.then(response ....
到
const promise = await axios.get ...
promise.then(response ....
不会工作...
它必须是
const response = await axios.get ...
不需要promise变量
即使如此,您仍仍在转换后的代码中使用承诺 ...现在,这只是由于无缘无故地使用async
关键字而有所不同
这是将您的(原始)代码转换为异步/等待的方式
请注意以下内容中“ Promise”一词的总缺失:
const dataService = (url, options, dataToPost) => {
return async (dispatch, getState) => {
const { requestAction, successAction, failureAction } = options.actions;
if (options.shouldRequest(getState())) {
const data = typeof dataToPost === 'string' ? { data: dataToPost } : dataToPost;
dispatch(requestAction());
try {
const response = dataToPost
? await axios.post(url, data, { withCredentials: true })
: await axios.get(url, { withCredentials: true });
if (response.status === 200) {
return dispatch(successAction(response, dispatch));
}
throw response;
} catch(error) {
if (error.response.status === 302) {
window.location = '/view';
}
dispatch(openErrorDialog());
return dispatch(failureAction(error));
}
}
throw new Error('FETCHING');
};
};
答案 1 :(得分:1)
await axios.post(url, data, { withCredentials: true })
不返回承诺,它返回请求的真实响应。
在使用async-await时不要使用then-catch,而应使用try-catch。 这是解决方法
try {
const response = dataToPost
? await axios.post(url, data, { withCredentials: true })
: await axios.get(url, { withCredentials: true });
if (response.status === 200) {
return dispatch(successAction(response, dispatch));
}
return Promise.reject(response);
} catch (err) {
if (error.response.status === 302) {
window.location = '/view';
}
dispatch(openErrorDialog());
return dispatch(failureAction(error));
}