所以,我當前的代碼看起來是這樣的:陣營併發API調用
組件:
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調用返回一個40的情況時,就做了這個4.
錯誤的是,併發調用到同一端點覆蓋我的參數。當api返回200時,我看不到問題。但現在當我測試返回一個404我可以清楚地看到問題。
問題即時得到就出在這裏:
this.props.getDescriptions(params);
params.gender = 'women';
如果我檢查控制檯日誌我的動作(//這裏)內,在這兩種情況下params.gender顯示「婦女」即使周圍的第一次的應該是'男'
我想這可以使用承諾修復?
讓我知道如果我不夠清楚。