我正在尝试使用 redux saga 查询多个休息端点,以获取与每个发现的网络关联的人类可读的名称,并用可选网络填充下拉列表。
我在执行此操作时遇到问题,我的 fork 不起作用。我不断收到错误消息:
TypeError: __webpack_require__.i(..) is not a function(...)
每个使用 all() 的示例我都发现在线使用调用并提前知道发出的每个请求。然而,从 API 来看,我尝试了这样的事情:
const pendingQueries = [];
for(networkId in discoveredNetworks) {
pendingQueries.push(fork(getApplicationName, networkId);
}
const queryResults = yield all(pendingQueries);
这失败了。从那以后我尝试了许多其他排列。通过测试,我能够验证我是否可以做到这一点:
const results = [];
for(networkId in discoveredNetworks) {
results.push(yield fork(getApplicationName, networkId));
}
如果有足够长的延迟,该方法将运行并完成,尽管这种方法显然不能保证 fork 方法将在我按照我想要的方式使用结果之前完成。不过,这似乎证实了问题出在我对 all 的使用上。
我的 all 命令有什么问题?
最佳答案
为什么不将每个请求包装在一个 promise 中并像这样调用它们:
var promises = []
for(networkId in discoveredNetworks) {
promises.push(new Promise((res, rej) => {
// code from getApplicationName goes here
// call res(result) on success and rej(error) on failure
}));
}
const results = yield call(Promise.all(promises))
关于javascript - 如何等待所有动态数量的 fork 完成 redux-saga?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46569278/