redux-saga 中的 Promise [英] Promises in redux-saga
问题描述
我在这里发现了同样的问题,但没有适当的我正在寻找的答案.
I found the same question here, but without a proper answer I am looking for.
我正在开发一个带有 CRUD 操作的简单应用程序.在编辑页面上,在组件挂载后(componentDidMount()
),应用程序会调度一个操作来检索特定的帖子详细信息:
I am developing a simple application with CRUD operations. On the edit page, after the component gets mounted (componentDidMount()
), the app dispatches an action to retrieve a specific post details:
dispatch({ type: FETCH_POST, id: 'post-id' })
我正在使用 redux-saga 并希望上述调用返回一个 Promise,以便我可以访问 API 响应.
I am using redux-saga and want the above call to return a Promise so that I can access the API response.
现在,没有回调/承诺,我最终在商店中定义了一个新状态(如 post_edited
)并将其连接/映射到组件中的道具以进行编辑页面.
Right now, without a callback/Promise, I ended up with defining a new state in store (like post_edited
) and connect/map it to props in the component for edit page.
处理这种情况的最佳方法是什么?
What would be the best possible way to deal with this kind of situation?
推荐答案
您能否提供有关您的问题的更多信息?我不确定我是否正确理解您的问题,但通常的做法是:
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
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);
}
这篇关于redux-saga 中的 Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!