如何在另一个redux-saga的末尾调用一个saga,例如在redux-thunk中进行异步等待

问题描述

我有两个传奇,一个获得了城市,另一个是这些城市的天气预报(根据ID专家的说法),我该如何做,以便在第一个传奇结束时处理第二个传奇? 调用我的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(){...}

我的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)

解决方法

您可以将componentDidMount更新为仅呼叫this.props.fetchCities。更新您的观察者功能mySaga使其包含

{
  ...,yield takeEvery(
    [FETCH_USERS_CITIES_SUCCESS],fetchUserCitiesWorker,)
}

这将使FETCH_USERS_CITIES_SUCCESS故事中的fetchUserCitiesWorker的有效载荷可用

,

您可以使用S <- seq(from = 0,to=80,by=2) TT <- S expand.grid(S,TT) 来实现它吗?

示例:完成saga fetchUser后,您将键入fetchUserSuccess。因此,您可以在后台运行一个任务,以随时检查获取用户完成的工作。

dim(expand.grid(S,TT))
[1] 1681    2