question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Abort request in `onRequest`

See original GitHub issue

Hi 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?

image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
burgaloncommented, Aug 1, 2018

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!

0reactions
klis87commented, Jul 26, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found