Get response of action as a promise (like redux-thunk) -
i moving redux-thunk redux-saga finding 1 deficiency.
with redux-thunk had typical way of doing "add requests":
try { downloadid = await dispatch(requestdownload('some_url')); } catch(ex) { console.log('download existed, request denied'); } that action return promise, wait on. request function (requestdownload above) either grant request, , resolve downloadid or reject, if download some_url existed.
how can in redux-saga? seems actions cannot return anything.
thanks
in redux-saga not using await yield in combination effects instead.
your code this:
// saga.js import { call, put } 'redux-saga/effects' import { requestdownloadsucceeded, requestdownloadfailed } './reducer.js' function* downloadrequestedflow() { try { const downloadid = yield call(requestdownload, 'some_url') yield put(requestdownloadsucceeded(downloadid)) } catch(error) { yield put(requestdownloadfailed(error)) } } // reducer.js ... export const requestdownloadsucceeded = downloadid => ({ type: request_download_succeeded, downloadid, }) export const requestdownloadfailed = error => ({ type: request_download_failed, error, }) note generator function * allows usage of yield. i'm using common requested, succeeded, failed pattern here.
i hope answer helpful.
Comments
Post a Comment