【发布时间】:2018-02-28 19:39:50
【问题描述】:
规范
根据官方文档 (https://github.com/gaearon/redux-thunk),我知道 redux thunk 允许按顺序调度一系列异步操作,如下所示:
function makeSandwichesForEverybody() {
return function (dispatch, getState) {
return dispatch(
makeASandwichWithSecretSauce('My Grandma')
).then(() =>
Promise.all([
dispatch(makeASandwichWithSecretSauce('Me')),
dispatch(makeASandwichWithSecretSauce('My wife'))
])
).then(() =>
dispatch(makeASandwichWithSecretSauce('Our kids'))
).then(() =>
dispatch(getState().myMoney > 42 ?
withdrawMoney(42) :
apologize('Me', 'The Sandwich Shop')
)
);
}
}
我的情况
但是,如果我有一个动态的动作数组想要遍历并调用呢?
let arrOfActions = [];
arrOfActions.push(action1);
arrOfActions.push(action2);
arrOfActions.push(action3);
如何使用 Promise 逻辑迭代地链接这些异步操作?为了最好地解释我的想法,我希望做这样的事情:
function thunkActionCreator() {
return function (dispatch, getState) {
for (let key of arrOfActions) {
dispatch(arrOfActions[key]()).then(
// run next action in arrOfActions here
)
}
}
}
这种函数调用的动态迭代是否可行?如果有,语法是什么?
为了验证您确实可以调用函数数组中的函数,这是我找到的资源:@987654322@
为什么是动态的动作数组?
可能有更好的方法来考虑这一点,但我尝试使用此实现的原因是因为我有一系列函数需要按特定顺序调用。该数组将存储在 Redux 的存储中,我不确定如何才能从头到尾依次调用一系列函数。任何其他想法都会有所帮助!
【问题讨论】:
标签: reactjs asynchronous promise redux redux-thunk