Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for options access to AbortControllers #27

Closed
jkauszler opened this issue Oct 27, 2022 · 2 comments
Closed

Request for options access to AbortControllers #27

jkauszler opened this issue Oct 27, 2022 · 2 comments
Labels
not a bug This will not be worked on

Comments

@jkauszler
Copy link

Love the simplicity and syntax of this library! The one thing preventing me from using it some production code is a way to work with Abort Controllers. Do you have a preferred method with this library (maybe I'm over looking something) or is that a feature worth adding to your roadmap?

@kwhitley
Copy link
Owner

kwhitley commented Jan 9, 2023

So I don't think we'll need to support them explicitly, for a couple reasons:

  1. This lib passes any options through to native fetch, which means you can absolutely use AbortController with it already! Just pass the signal as an option into the fetcher options (3rd) param like you would with any other fetch request...
  2. While the native AbortController syntax is IMO verbose and could use some abstraction like the rest of fetch, I have to assume the need to abort requests is still rather fringe, and not a mainstream need. Thus, to abstract this further for nice aborts, we'd be adding a bunch of precious bytes to a codebase where the vast majority would never be able to benefit from the cost. Probably better to publish a separate wrapper library or transformRequest plugin that joins the two ideas... :)

@kwhitley kwhitley added the not a bug This will not be worked on label Sep 12, 2023
@kwhitley
Copy link
Owner

Also should note that since we pass through options, to native fetch you can use Abort Controllers just like you could with fetch.

AbortController with native fetch

// Step 1: Create an instance of AbortController
const controller = new AbortController()
const signal = controller.signal

// Step 2: Initiate a fetch request and pass the signal as an option
fetch('https://api.example.com/data', { signal })
  .then(response => response.json())
  .then(console.log)
  .catch(error => {
    if (error.name === 'AbortError') {
      console.log('Fetch aborted')
    } else {
      console.error('Fetch error:', error)
    }
  })

// Step 3: Abort the fetch request (this could be triggered by a user action, for example)
setTimeout(() => controller.abort(), 5000)

AbortController with itty-fetcher

// Step 1: Create an instance of AbortController
const controller = new AbortController()
const signal = controller.signal

// Step 2: Initiate a fetch request and pass the signal as an option (3rd argument)
fetcher()
  .get('https://api.example.com/data', {}, { signal })
  .then(console.log)
  .catch(error => {
    if (error.name === 'AbortError') {
      console.log('Fetch aborted')
    } else {
      console.error('Fetch error:', error)
    }
  })

// Step 3: Abort the fetch request (this could be triggered by a user action, for example)
setTimeout(() => controller.abort(), 5000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not a bug This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants