Promises in redux-saga

后端 未结 3 869
攒了一身酷
攒了一身酷 2021-02-14 12:26

I found the same question here, but without a proper answer I am looking for.

I am developing a simple application with CRUD operations. On the edit page, after the comp

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-14 13:20

    Could you please provide more information about your issue? I'm not sure if I understand your issue properly, but the common practice is:

    API.js

    function apiCallToFetchPost(id) {
      return Promise.resolve({name: 'Test});
    }
    

    postSaga.js

    function* fetchPostSaga({id}) {
      try {
        const request = yield call(apiCallToFetchPost, id);
        // -> in post reducer we will save the fetched data for showing them later 
        yield put({type: FETCH_POST_SUCCESS, payload: request}); 
      } catch (error) {
        yield put({type: FETCH_POST_SUCCESS_FAILURE, error})
      }
    }
    
    export function* onBootstrap() {
      yield takeLatest(FETCH_POST, fetchPostSaga);
    }
    

提交回复
热议问题