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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -