解决redux saga生成器内部的承诺

问题描述

我有一个生成器,可以在其中更新对象。在这里,我需要调用返回promise的多个函数,所以我使用yield all和(肯定的双关语)都很好。像这样:

function* updateTheThing() {
  ...
  const [resultA,resultB,resultC] = yield all([funcA(),funcB(),funcC()]);
  ...
}

这适用于更新单个项目,但我想更新一系列项目。我的想法是使用map和对所有项目进行map,但是问题是我无法在map内屈服。在做所有需要做的事情之前,我已经做过类似的事情:

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是Promise,但我需要解析的值,因为我需要在map语句中进一步使用它。

也许我的方法是错误的,但是我很沮丧。有人有建议吗?

解决方法

好的,在this question中尼古拉斯·塔的回答的帮助下,我能够重新思考并重新编写我的代码以按预期工作。如果有人觉得有帮助,我将在此处添加我的解决方案。

所以我决定使用的部分问题是用他们的话来说,创建一个迷你传奇,所以我的代码现在是这样的:

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}`);
  }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...