在 redux saga 生成器的映射中解决 promise(s)
Resolve promise(s) inside map of redux saga generator
我有一个更新对象的生成器。在这里我需要调用 return 承诺的多个函数,所以我使用 yield all 并且(明确的双关语)一切都很好。像这样:
function* updateTheThing() {
...
const [resultA, resultB, resultC] = yield all([funcA(), funcB(), funcC()]);
...
}
这适用于更新单个项目,但我想更新一组项目。我的想法是在项目上使用地图和地图,但问题是我无法在地图内屈服。我之前做过类似的事情,当时我需要做的就是像这样调用一些 API:
const promises = things.map(thing => {
return call(api.someEndpoint);
});
const data = yield all(promises);
但是我不能在这里这样做,因为我没有使用 redux saga 的调用。我现在的代码是这样的,但它不起作用:
function* updateAllTheThings() {
try {
const updatedThings = things.map(thing => {
const resultA = funcA(); // resultA is a promise
const resultB = funcB(); // resultB is a promise
const resultC = funcC(); // resutlC is a promise
});
} catch (error) {
console.log(`error updating all the things: ${error}`);
}
}
resultA、resultB 和 resultC 是承诺,但我需要解析值,因为我需要在 map 语句中进一步使用它。
也许我的做法是错误的,但我很困惑。有人有什么建议吗?
好的,在 Nicolas Tower 在 this question 中的回答的帮助下,我能够 re-think 和 re-write 我的代码按预期工作。如果有人觉得有用,我会在这里添加我的解决方案。
所以我决定使用的问题部分是,用他们的话说,创建一个迷你传奇,所以我的代码现在是这样的:
function* updateAllTheThings() {
try {
const promisedThings = things.map(thing => {
return call (function* () {
const resultA = funcA(); // resultA is a promise
const resultB = funcB(); // resultB is a promise
const resultC = funcC(); // resutlC is a promise
});
});
const updatedThings = yield all(promisedThings);
...
} catch (error) {
console.log(`error updating all the things: ${error}`);
}
}
我有一个更新对象的生成器。在这里我需要调用 return 承诺的多个函数,所以我使用 yield all 并且(明确的双关语)一切都很好。像这样:
function* updateTheThing() {
...
const [resultA, resultB, resultC] = yield all([funcA(), funcB(), funcC()]);
...
}
这适用于更新单个项目,但我想更新一组项目。我的想法是在项目上使用地图和地图,但问题是我无法在地图内屈服。我之前做过类似的事情,当时我需要做的就是像这样调用一些 API:
const promises = things.map(thing => {
return call(api.someEndpoint);
});
const data = yield all(promises);
但是我不能在这里这样做,因为我没有使用 redux saga 的调用。我现在的代码是这样的,但它不起作用:
function* updateAllTheThings() {
try {
const updatedThings = things.map(thing => {
const resultA = funcA(); // resultA is a promise
const resultB = funcB(); // resultB is a promise
const resultC = funcC(); // resutlC is a promise
});
} catch (error) {
console.log(`error updating all the things: ${error}`);
}
}
resultA、resultB 和 resultC 是承诺,但我需要解析值,因为我需要在 map 语句中进一步使用它。
也许我的做法是错误的,但我很困惑。有人有什么建议吗?
好的,在 Nicolas Tower 在 this question 中的回答的帮助下,我能够 re-think 和 re-write 我的代码按预期工作。如果有人觉得有用,我会在这里添加我的解决方案。
所以我决定使用的问题部分是,用他们的话说,创建一个迷你传奇,所以我的代码现在是这样的:
function* updateAllTheThings() {
try {
const promisedThings = things.map(thing => {
return call (function* () {
const resultA = funcA(); // resultA is a promise
const resultB = funcB(); // resultB is a promise
const resultC = funcC(); // resutlC is a promise
});
});
const updatedThings = yield all(promisedThings);
...
} catch (error) {
console.log(`error updating all the things: ${error}`);
}
}