FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add support for request cancellation using AbortController by melwyn95 · Pull Request #47 · developit/redaxios · GitHub

Repository navigation

Add support for request cancellation using AbortController - #47

Open
melwyn95 wants to merge 10 commits into
developit:masterfrom
melwyn95:request-cancellation
Open

melwyn95 wants to merge 10 commits into
developit:masterfrom
melwyn95:request-cancellation

Conversation

Copy link
Copy Markdown

As per #11 added support for request cancellation using the abort controller exposed as CancelToken.

Added test for the same.

@developit I don't know if this the correct way to do this, please let me know what to if I'm wrong

Copy link
Copy Markdown
Collaborator

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

Copy link
Copy Markdown
Author

@googlebot I signed it!

Copy link
Copy Markdown
Collaborator

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

developit left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Perfect!!

n3tr commented Jul 24, 2020

Copy link
Copy Markdown

Do you think it is better to add support cancelToken API similar to axios cancel token instead of passing abortController signal directly?

Example from axios

const CancelToken = axios.CancelToken;
const source = CancelToken.source();

axios.get('/user/12345', {
  cancelToken: source.token
}).catch(function (thrown) {
  if (axios.isCancel(thrown)) {
    console.log('Request canceled', thrown.message);
  } else {
    // handle error
  }
});

axios.post('/user/12345', {
  name: 'new name'
}, {
  cancelToken: source.token
})

// cancel the request (the message parameter is optional)
source.cancel('Operation canceled by the user.');

We can create a CancelToken function that wrap around the AbortController and return signal as a cancel token, what do you think?

Copy link
Copy Markdown

@developit @n3tr To support the axios API I think we'll have to implement the CancelToken like this

function CancelToken(fn) {
	const ac = new AbortController();
	fn && fn(() => ac.abort());
	return ac.signal;
}
CancelToken.source = () => {
	const ac = new AbortController();
	return {
		token: ac.signal,
		cancel: () => ac.abort()
	};
};

By implementing CancelToken this we can support both the use-cases of cancel token
Use Case 1

const CancelToken = axios.CancelToken;
let cancel;

axios.get('/user/12345', {
  cancelToken: new CancelToken(function executor(c) {
    // An executor function receives a cancel function as a parameter
    cancel = c;
  })
});

// cancel the request
cancel();

Use Case 2

const CancelToken = axios.CancelToken;
const source = CancelToken.source();

axios.get('/user/12345', {
  cancelToken: source.token
}).catch(function (thrown) {
  if (axios.isCancel(thrown)) {
    console.log('Request canceled', thrown.message);
  } else {
    // handle error
  }
});

axios.post('/user/12345', {
  name: 'new name'
}, {
  cancelToken: source.token
})

// cancel the request (the message parameter is optional)
source.cancel('Operation canceled by the user.');

I have tested this locally So if you want I can push this implementation of CancelToken

Copy link
Copy Markdown
Owner

Looks good. I will have to golf it down a lot, but it's nice to see what the API is supposed to be.

melwyn95 commented Jul 25, 2020 •
edited
Loading

Copy link
Copy Markdown
Author

@developit I pushed the changes, thought a lot about golfing it down, but no breakthrough...

melwyn95 closed this Aug 30, 2020
melwyn95 reopened this Aug 30, 2020

Copy link
Copy Markdown
Author

Merged master and fixed conflicts

Copy link
Copy Markdown
Owner

Thanks for putting in the effort to keep this moving forward @melwyn95, I'll take another look!

Copy link
Copy Markdown

Some great work here - will this be merged into main branch soon?

donlion commented Sep 1, 2022

Copy link
Copy Markdown
Contributor

@developit Any idea when this will be merged? Or is there something we can help with in order to get this ready for merge?

quicoto added a commit to quicoto/redaxios that referenced this pull request Jan 24, 2023

Copy link
Copy Markdown

Hi @developit @melwyn95

Any plans to merge this?

Sreejit7 commented Oct 9, 2023

Copy link
Copy Markdown

@developit @melwyn95 bumping this

1 similar comment

SergkeiM commented Feb 9, 2024

Copy link
Copy Markdown

@developit @melwyn95 bumping this

osbre pushed a commit to osbre/redaxios2 that referenced this pull request Jan 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants


Back | FazBrowse Home | New Git URL