"call" helper(effect) 如何在 redux-saga 中发挥作用 return 解析值而不是承诺?
How "call" helper(effect) function in redux-saga return the resolved value instead a promise?
我不明白调用方法的机制是什么,它自然会产生一个承诺,而是 return 该承诺的解析值。
第 14 行:
import { put, call, takeLatest } from "redux-saga/effects";
import * as api from "./api";
//generate watchers
function* rootSaga() {
//define watcher
yield takeLatest("FETCH_TASKS_STARTED", fetchTasks);
}
//watcher is subProgram.They are executed when an particular action of the dispatched
function* fetchTasks() {
try {
yield put({
type: "REQUEST_STARTED",
});
const { data } = yield call(api.fetchTasks);
yield put({
type: "FETCH_TASKS_SUCCEED",
payLoad: { tasks: data },
});
} catch (e) {
yield put({
type: "REQUEST_FAILED",
payLoad: { error: e.message },
});
}
}
export default rootSaga;
调用效果源码can be found here
调用效果首先调用您要求它调用的任何内容,然后检查返回的内容。如果您 return a promise(例如,如果您将 call
与异步函数一起使用),它会等待该承诺通过名为 resolvePromise
的辅助函数来解决。 resolvePromise 基本上只是调用 .then
承诺,传递一个回调。回调是知道如何恢复 saga 的函数,它将通过调用 .next()
.
来完成
如果你return an iterator (eg, if you use call
with a saga), it hands control over to the proc
function, which is the function responsible for running sagas. That code also checks for promises and waits for them to resolve, on this line
我不明白调用方法的机制是什么,它自然会产生一个承诺,而是 return 该承诺的解析值。
第 14 行:
import { put, call, takeLatest } from "redux-saga/effects";
import * as api from "./api";
//generate watchers
function* rootSaga() {
//define watcher
yield takeLatest("FETCH_TASKS_STARTED", fetchTasks);
}
//watcher is subProgram.They are executed when an particular action of the dispatched
function* fetchTasks() {
try {
yield put({
type: "REQUEST_STARTED",
});
const { data } = yield call(api.fetchTasks);
yield put({
type: "FETCH_TASKS_SUCCEED",
payLoad: { tasks: data },
});
} catch (e) {
yield put({
type: "REQUEST_FAILED",
payLoad: { error: e.message },
});
}
}
export default rootSaga;
调用效果源码can be found here
调用效果首先调用您要求它调用的任何内容,然后检查返回的内容。如果您 return a promise(例如,如果您将 call
与异步函数一起使用),它会等待该承诺通过名为 resolvePromise
的辅助函数来解决。 resolvePromise 基本上只是调用 .then
承诺,传递一个回调。回调是知道如何恢复 saga 的函数,它将通过调用 .next()
.
如果你return an iterator (eg, if you use call
with a saga), it hands control over to the proc
function, which is the function responsible for running sagas. That code also checks for promises and waits for them to resolve, on this line