代码之家  ›  专栏  ›  技术社区  ›  arunmmanoharan

将承诺转换为async/await-Javascript

  •  3
  • arunmmanoharan  · 技术社区  · 5 年前

    使用承诺:

    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'));
    	    };
    	};

    2 回复  |  直到 5 年前
        1
  •  7
  •   Jaromanda X    5 年前

    如果您真的想改变承诺->async/await然后更改如下:

    首先,您不希望dataService async

    第二,改变

      const promise = axios.get ...
      promise.then(response ....
    

      const promise = await axios.get ...
      promise.then(response ....
    

    行不通。。。

    必须是这样

    const response = await axios.get ...
    

    不需要promise变量

    仍在使用承诺 异步

    下面是如何将(原始)代码转换为async/await

    请注意以下内容中完全没有“承诺”一词:

    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');
        };
    };
    
        2
  •  3
  •   Gianfranco Fertino    5 年前

    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));
    }