[英]How to call one saga at the end of another redux-saga like a async await in redux-thunk
I have two sagas, one gets the cities, the other is the weather forecast for these cities (according to ID specialists), how can I make it so that the second saga is processed at the end of the first?我有两个传奇,一个获取城市,另一个是这些城市的天气预报(根据 ID 专家的说法),我怎样才能使第二个传奇在第一个结束时处理? method in which a call my sagas:
调用我的传奇的方法:
async componentDidMount() {
this.props.navigation.setOptions(this.navigationOptions);
//sagas call
await this.props.fetchCities();
await this.fetchForecastsHandler(this.props.userCities);
}
...some code
fetchForecastsHandler(cities: ICIty[]) {
const ids = cities.map((el) => el.id);
this.props.fetchForecasts(ids);
}``
...some code
render(){...}
My index.ts saga我的 index.ts 传奇
export function* mySaga() {
yield takeEvery(types.FETCH_USERS_CITIES, fetchUserCitiesWorker);
yield takeEvery(types.REMOVE_CITY, removeUserCitiesWorker);
yield takeEvery(types.SEARCH_CITY, searchUserCitiesWorker);
yield takeEvery(types.FETCH_FORECAST, fetchForecastWorker);
yield takeEvery(types.ADD_NOTIFICATION, addNotificationWorker);
yield takeEvery(types.EDIT_NOTIFICATION, editNotificationWorker);
yield takeEvery(types.DELETE_NOTIFICATION, deleteNotificationsWorker);
yield takeEvery(types.FETCH_NOTIFICATION, fetchNotificationsWorker);
}
**FeychCityWorker saga:**
export function* fetchUserCitiesWorker(callback?:any) {
try {
yield put({ type: types.FETCH_USERS_CITIES_START });
//const user = yield call(Api.fetchUser, action.payload.userId);
yield delay(2000,true)
yield put({ type: types.FETCH_USERS_CITIES_SUCCESS, payload: userCities });
console.log("fetchUserCitiesWorker worked sucess")
//callback?callback():null
} catch (e) {
yield put({ type: types.FETCH_USERS_CITIES_ERROR, error: e.message });
}
}
**also storage settings just in case:**
const sagaMiddleware = createSagaMiddleware()
export const store = createStore(
reducer,
applyMiddleware(sagaMiddleware)
)
export const action = (type:string) => store.dispatch({type})
sagaMiddleware.run(mySaga)
You can update your componentDidMount
to call only this.props.fetchCities
.您可以更新您的
componentDidMount
以仅调用this.props.fetchCities
。 Update your watcher function mySaga
to include更新您的观察者函数
mySaga
以包含
{
...,
yield takeEvery(
[FETCH_USERS_CITIES_SUCCESS],
fetchUserCitiesWorker,
)
}
This will make the payload of FETCH_USERS_CITIES_SUCCESS
available in the fetchUserCitiesWorker
saga这将使
FETCH_USERS_CITIES_SUCCESS
的有效负载在fetchUserCitiesWorker
传奇中可用
you can use fork
to implement it?你可以用
fork
来实现吗?
example: you have type fetchUserSuccess when saga fetchUser is done.例如:当 saga fetchUser 完成时,您输入了 fetchUserSuccess 。 So you can run one task run on the background to check anytime fetch user done to make something.
因此,您可以在后台运行一项任务,以随时检查 fetch 用户完成的操作。
function* waitFetchUserDone() {
while (true) {
yield take(fetchUser.SUCCESS);
// yield something
// to do something
}
}
yield fork(waitFetchUserDone);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.