redux 调度 ActionCreator 上的绑定变量
Binding variable on redux dispatch ActionCreator
我有一个 promise 数组,我正试图将新的 promise 推入另一个 dispatch.then 函数内的那个数组,但似乎该数组总是超出范围
load(params, auth) {
return dispatch => {
const { passage, versions, language_tag } = params
let promises = []
versions.forEach((id) => {
// get the version info, and then pass it along
dispatch(ActionCreators.version({ id: id })).bind(promises).then((version) => {
promises.push(dispatch(ActionCreators.passages({
id: id,
references: [passage],
versionInfo: {
local_abbreviation: version.abbreviation,
local_title: version.title,
id: version.id,
},
})))
})
})
//
promises.push(dispatch(ActionCreators.configuration()))
promises.push(dispatch(ActionCreators.byRef({ language_tag })))
console.log(promises.length)
return Promise.all(promises)
}
},
我尝试了几种不同的方法,例如在版本循环内部的调度之前设置 var that = this
,以及此处显示的内容,尝试在调度中使用 .bind(promises)
。
promises.length 总是 2,(因为两个实际上被推到了底部)。我可以在 .then
中控制语句,所以我知道它正在执行,但调度不会在 promises 数组中结束。
我很可能以错误的方式思考了调度函数。
如有任何帮助,我们将不胜感激!
问题在于,由于您是在 then()
上添加承诺,因此在添加承诺时您已经返回了数组。所以他们确实被添加了,但为时已晚。
相反,试试这个:
load(params, auth) {
return dispatch => {
const { passage, versions, language_tag } = params;
let promises = [];
versions.forEach((id) => {
// get the version info, and then pass it along
promises.push(dispatch(ActionCreators.version({ id: id })).then((version) => {
return dispatch(ActionCreators.passages({
id: id,
references: [passage],
versionInfo: {
local_abbreviation: version.abbreviation,
local_title: version.title,
id: version.id,
},
}));
}));
});
//
promises.push(dispatch(ActionCreators.configuration()));
promises.push(dispatch(ActionCreators.byRef({ language_tag })));
console.log(promises.length);
return Promise.all(promises)
}
}
我有一个 promise 数组,我正试图将新的 promise 推入另一个 dispatch.then 函数内的那个数组,但似乎该数组总是超出范围
load(params, auth) {
return dispatch => {
const { passage, versions, language_tag } = params
let promises = []
versions.forEach((id) => {
// get the version info, and then pass it along
dispatch(ActionCreators.version({ id: id })).bind(promises).then((version) => {
promises.push(dispatch(ActionCreators.passages({
id: id,
references: [passage],
versionInfo: {
local_abbreviation: version.abbreviation,
local_title: version.title,
id: version.id,
},
})))
})
})
//
promises.push(dispatch(ActionCreators.configuration()))
promises.push(dispatch(ActionCreators.byRef({ language_tag })))
console.log(promises.length)
return Promise.all(promises)
}
},
我尝试了几种不同的方法,例如在版本循环内部的调度之前设置 var that = this
,以及此处显示的内容,尝试在调度中使用 .bind(promises)
。
promises.length 总是 2,(因为两个实际上被推到了底部)。我可以在 .then
中控制语句,所以我知道它正在执行,但调度不会在 promises 数组中结束。
我很可能以错误的方式思考了调度函数。
如有任何帮助,我们将不胜感激!
问题在于,由于您是在 then()
上添加承诺,因此在添加承诺时您已经返回了数组。所以他们确实被添加了,但为时已晚。
相反,试试这个:
load(params, auth) {
return dispatch => {
const { passage, versions, language_tag } = params;
let promises = [];
versions.forEach((id) => {
// get the version info, and then pass it along
promises.push(dispatch(ActionCreators.version({ id: id })).then((version) => {
return dispatch(ActionCreators.passages({
id: id,
references: [passage],
versionInfo: {
local_abbreviation: version.abbreviation,
local_title: version.title,
id: version.id,
},
}));
}));
});
//
promises.push(dispatch(ActionCreators.configuration()));
promises.push(dispatch(ActionCreators.byRef({ language_tag })));
console.log(promises.length);
return Promise.all(promises)
}
}