Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 响应并发API调用_Javascript_Reactjs_Redux_Promise - Fatal编程技术网

Javascript 响应并发API调用

Javascript 响应并发API调用,javascript,reactjs,redux,promise,Javascript,Reactjs,Redux,Promise,因此,我当前的代码如下所示: export function getDescriptions(params = { brand: null, category: null, gender: null }) { return (dispatch) => { dispatch(requestDescription()); return request .get(`${getApiUrl()}plp?${QueryString.stringify(params)}

因此,我当前的代码如下所示:

export function getDescriptions(params = { brand: null, category: null, gender: null }) {
return (dispatch) => {
    dispatch(requestDescription());
    return request
        .get(`${getApiUrl()}plp?${QueryString.stringify(params)}`)
        .end((err, res) => {
            if (err && res.statusCode !== 404) {
                ...
            }
            if (res.statusCode === 404) {
                // HERE: 
                console.log(params.gender); 
                return dispatch(receiveEmptyDescription(params));
            }
            return dispatch(receiveDescription(res.body));
        });
};
组成部分:

requestDescriptions(params, bothGender) {
    if (bothGender) {
        // men request
        params.gender = 'men';
        this.props.getDescriptions(params);
        // women request
        params.gender = 'women';
        this.props.getDescriptions(params);
    } else {
        this.props.getDescriptions(params);
    }
}
我的行动是这样的:

export function getDescriptions(params = { brand: null, category: null, gender: null }) {
return (dispatch) => {
    dispatch(requestDescription());
    return request
        .get(`${getApiUrl()}plp?${QueryString.stringify(params)}`)
        .end((err, res) => {
            if (err && res.statusCode !== 404) {
                ...
            }
            if (res.statusCode === 404) {
                // HERE: 
                console.log(params.gender); 
                return dispatch(receiveEmptyDescription(params));
            }
            return dispatch(receiveDescription(res.body));
        });
};
}

所以到目前为止,我还没有注意到这个错误,但当我开始测试一个场景时,两个API调用都返回404

错误是对同一端点的并发调用正在覆盖我的参数。当API返回200时,我看不出问题所在。但现在当我测试返回404时,我可以清楚地看到问题所在

我遇到的问题在于:

this.props.getDescriptions(params);
params.gender = 'women';
如果我检查操作中的控制台日志(
//此处
),在这两种情况下,
参数。性别
显示
'women'
,即使第一次应该是
'men'

我想这可以用承诺来解决


如果我不够清楚,请告诉我。

问题并不在于您是否提出了并行请求,或者您是否获得了HTTP2XX或HTTP4XX

您总是看到
gender
等于
'women'
的原因是,您对两个请求都使用了相同的对象实例,即
params

发生的情况是,您将
params.gender
设置为
'men'
,第一个请求触发,将
params.gender
设置为
'women'
,第二个请求触发

在任何请求完成之前,上述所有步骤都是同步进行的

这意味着在任何请求完成之前,
params.gender
早就等于
'women'

只需向每个操作发送不同的对象

requestDescriptions(params, bothGender) {
    if (bothGender) {
        // men request
        this.props.getDescriptions({ ...params, gender: 'men' });

        // women request
        this.props.getDescriptions({ ...params, gender: 'women' });
    } else {
        this.props.getDescriptions(params);
    }
}
既然你已经在使用Redux,你应该知道变异是Baaad