Abort request in `onRequest`
See original GitHub issueHi Konrad,
Thank you for publishing redux-saga-requests!
I’m trying to abort requests in the onRequest hook and I’m either not sure what’s the right way to do it, or maybe the redux-saga-requests onRequest API should be changed to support it:
Here’s the scenario:
function* onRequestSaga(axiosInstance, request, action) {
if (action.confirm) {
const confirmed = yield call(confirmSaga, action.confirm);
if (!confirmed) {
// option 1 - yields a console exception
throw new CancelRequest('Aborted by confirmation dialog');
// option 2 - return error - request still tried to be dispatched and the exception is swollen - see attached screenshot below
return { error: new CancelRequest('Aborted by confirmation dialog'))
// dispatch abort - doesn't abort the request
yield put({
type: abort(action.type),
meta: {
requestAction: action,
},
})
}
}
return request;
}
any ideas?
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (7 by maintainers)
Top Results From Across the Web
Abort request in `onRequest` · Issue #146 - GitHub
I'm trying to abort requests in the onRequest hook and I'm either not sure what's the right way to do it, or maybe...
Read more >puppeteer.Request.abort JavaScript and Node.js ... - Tabnine
Aborts request. To use this, request interception should be enabled with `page.setRequestInterception`. Most used puppeteer functions. launch.
Read more >Request Interception - Puppeteer
Always assume that an unknown handler may have already called abort/continue/respond . Even if your handler is the only one you registered, 3rd...
Read more >How to Cancel a Request in JavaScript - Level Up Coding
The XMLHttpRequest.abort() method aborts the request if it has already been sent. When a request is aborted, its readyState is changed to XMLHttpRequest....
Read more >how to cancel/abort ajax request in axios - Stack Overflow
The axios cancel token API is based on the withdrawn cancelable promises proposal.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
hey @klis87 - I’ve followed you suggestion for wrapping the requestAction with a prior confirmAction which seems to be working great and can also be applied to none redux-requests actions, which is great. Thank you for your advice!
Great it works.
If authorization token is missing, you shouldnt even try to dispatch request which is doomed to fail. Re adding request token globally,
onRequest
is the best place to add it, see readme example in interceptors paragraph.And if you have an auth token but it is invalid, your server will probably raise some 401/403 http error which you could catch in
onError
saga.Anyway, requests aborts is not dedicated for your use cases. Aborts are nice if for instance u fetch 1st page of some list, and before it finished a new request for page 2 was sent. Then not only 1st page can be aborted because it is not needed anymore, it could also cause a race condition where 2nd page request would be finished before 1st, resulting in showing items for 1st page while user wanted to see 2nd! This is actually what this library is doing for u. But you should never think about aborting request which shouldnt be sent at all.