// example 1
import { take, fork, call, put } from 'redux-saga/effects';
// The worker: perform the requested task
function* fetchUrl(url) {
const data = yield call(fetch, url); // 指示中间件调用 fetch 异步任务
yield put({ type: 'FETCH_SUCCESS', data }); // 指示中间件发起一个 action 到 Store
}
// The watcher: watch actions and coordinate worker tasks
function* watchFetchRequests() {
while(true) {
const action = yield take('FETCH_REQUEST'); // 指示中间件等待 Store 上指定的 action,即监听 action
yield fork(fetchUrl, action.url); // 指示中间件以无阻塞调用方式执行 fetchUrl
}
}
action可以由page 或 saga(异步返回数据后发action)进行调用,然后交由reducer更新store