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

AbortAddon: setTimeout doesn't work in combination with own AbortController instance #256

Open
adriaanmeuris opened this issue Dec 13, 2024 · 2 comments
Labels

Comments

@adriaanmeuris
Copy link

adriaanmeuris commented Dec 13, 2024

Timeouts and aborting requests work fine when using .controller:

// Aborts after 1 second timeout
const [controller, w] = wretch(url).addon(AbortAddon()).get().setTimeout(1000).controller();

// Aborts after 0.5 second using `.abort`
const [controller, w] = wretch(url).addon(AbortAddon()).get().setTimeout(1000).controller();  
setTimeout(() => { controller.abort(); }, 500);

However when you want to provide a controller yourself, the implementation is broken:

// Aborts after 1 second timeout, but can't be aborted before because we don't provide a controller
await wretch(url).addon(AbortAddon()).get().setTimeout(1000);

// Doesn't abort after 0.5 second timeout (even though configured so), but can be aborted using `.abort`
const controller = new AbortController();
await wretch(url).addon(AbortAddon()).signal(controller).get().setTimeout(500);
setTimeout(() => { controller.abort(); }, 1000);

Additionally, I don't this it's currently possible to differentiate between a configured timeout error, and manually aborting it (they both trigger the onAborted catcher)

@elbywan
Copy link
Owner

elbywan commented Dec 13, 2024

Hey @adriaanmeuris,

// Doesn't abort after 0.5 second timeout (even though configured so)
await wretch(url).addon(AbortAddon()).signal(controller).get().setTimeout(500);

I believe you have to explicitly pass your custom controller as an argument: .setTimeout(500, controller)

If you use a custom AbortController associated with the request, pass it as the second argument.

https://elbywan.github.io/wretch/api/interfaces/addons_abort.AbortResolver.html#setTimeout

I don't this it's currently possible to differentiate between a configured timeout error, and manually aborting it

They are both equivalent indeed, since setTimeout calls controller.abort() under the hood.

@adriaanmeuris
Copy link
Author

Thanks for clarifying. Is .signal(controller) still required if the controller is passed as a second argument to setTimeout?

I need to distinguish manual aborts (e.g. aborting irrelevant requests before they resolve) from actual timeouts, especially for error reporting. Any advice on this, or would you consider a PR that allow to explicitly separate the two cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants