Questions about react Asynchronous action

here is the code that does not use asynchronous action

const mapDispatchToProps = (dispatch, ownProps) => {
    return {
        onLoadSuccessHandle: (weather) => {
            dispatch(loadSuccess(weather))
        },
        onLoadFailureHandle: () => {
            dispatch(loadFailure())
        }
    }
}


    componentDidMount() {
        const cityCode = 101010100

        const apiUrl = `/data/cityinfo/${cityCode}.html`;

        fetch(apiUrl).then((response) => {
            if (response.status !== 200) {
                throw new Error("Fail to get response with status " + response.status);
            }
    
            response.json().then((responseJson) => {
                this.props.onLoadSuccessHandle(responseJson.weatherinfo)
            }).catch((error) => {
                this.props.onLoadFailureHandle()
            });
        }).catch((error) => {
            this.props.onLoadFailureHandle()
        });
    }

here is an asynchronous action

export const loadWeather = () => {
    return (dispatch) => {
        dispatch(loadStarted())

        const cityCode = 101010100
        const apiUrl   = `/data/cityinfo/${cityCode}.html`
        fetch(apiUrl).then((response) => {
            if (response.status !== 200) {
                throw new Error("Fail to get response with status " + response.status);
            }
    
            response.json().then((responseJson) => {
                dispatch(loadSuccess(responseJson.weatherinfo))
            }).catch((error) => {
                dispatch(loadFailure())
            });
        }).catch((error) => {
            dispatch(loadFailure())
        });


    }
}

the asynchronous action mode above encapsulates the code for initiating the request into action, that is to say, asynchronous action is only for the purpose of unifying the interface of dispatch?


this makes your components cleaner, separates this url request from presentation, and facilitates reuse and unit testing

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38d94-e697.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38d94-e697.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?