我在让 Saga 使用可与 Promise 配合使用的 API 时遇到问题。
const result = yield call(
() => {
return this.getDays().then((_result) => {
console.log('result inside');
console.log(_result);
})}
);
console.log('result outside');
console.log(result);
当我在 then() 中读取 _result 的值时,它会打印正确的值,但我似乎无法使其在yield调用之外工作以进一步将其传递给操作调度(yield put)。
按照类似问题的引导,我尝试了很多东西:
我尝试将 _result 的返回值以及 Promise.resolve(_result) 放入 then() 中。
我还尝试返回整个yield调用并将一个变量放在外面以在then()中分配它,但似乎没有任何效果。
这是我第一次使用 Promises 和生成器函数以及 Saga、Redux 和 React,所以我肯定错过了一些东西。
另外,then() 中的 console.log() 会在最后的 console.log() 之后打印。
有什么想法吗?谢谢。
最佳答案
redux-saga 中的
call
需要一个返回 Promise 的函数。
const result = yield call(
() => new Promise((resolve) => {
this.getDays().then((_result) => {
resolve(_result);
});
});
);
关于javascript - 在 Redux-saga 中,如何从 Promise 的 Yield 调用中获取结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61481488/