React native 如何使用React Native的收益率内部映射
我的目标是在React native 如何使用React Native的收益率内部映射,react-native,redux-saga,React Native,Redux Saga,我的目标是在map中执行分叉函数 以下是我尝试过的: function* doSomethingWithItem(item) {} yield all(items.map(item => { // ... some item checking return fork(doSomethingWithItem, item); })); 还尝试使用yield fork(),但出现错误“yield是一个保留字…” 未调用doSomethingWithItem() 感
map
中执行分叉函数
以下是我尝试过的:
function* doSomethingWithItem(item) {}
yield all(items.map(item => {
// ... some item checking
return fork(doSomethingWithItem, item);
}));
还尝试使用yield fork()
,但出现错误“yield是一个保留字…”
未调用doSomethingWithItem()
感谢您的帮助。由于
map
使用lambda函数,因此您实际上无法直接从中产生一些结果
yield all
是一种正确的方法,但是与fork
不同,call
效果在这种情况下看起来更合适,因为它是阻塞的,因此保留了项目处理顺序(如果这很重要):
我还检查了您的代码,
doSomethingWithItem
在我的环境中工作。尝试用Try/catch包装您的代码,可能您有一个错误,使saga停止。是的,在catch
删除错误后,错误是无关的。谢谢~
function * doSomethingWithItem ( item ) {
console.log('doSomethingWithItem', item)
}
function * doSomethingWithAllItems ( items ) {
console.log('doSomethingWithAllItems')
yield all(items.map(item =>
call(doSomethingWithItem, item),
))
console.log('done doSomethingWithAllItems')
}
function * mySaga () {
yield call(doSomethingWithAllItems, [1, 2, 3, 4, 5])
}