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

Support AbortController for aborting fetch operations #64

Closed
stefan-guggisberg opened this issue Jun 29, 2020 · 1 comment
Closed

Support AbortController for aborting fetch operations #64

stefan-guggisberg opened this issue Jun 29, 2020 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@stefan-guggisberg
Copy link
Contributor

stefan-guggisberg commented Jun 29, 2020

Is your feature request related to a problem? Please describe.
AbortController is part of the DOM Living standard. It allows to abort an in-progress fetch operation in a standard way via the signal option. Specifically it allows to apply a timeout to a fetch operation in a standard-compliant portable way.

Describe the solution you'd like
Export AbortController, AbortError and optionally a small helper timeoutSignal for specifying a timeout signal to support the following code snippets:

const { fetch, AbortController, AbortError } = require('@adobe/helix-fetch`);

const controller = new AbortController();
setTimeout(() => controller.abort(), 1000);

try {
  await fetch('https://httpbin.org/delay/2', { signal: controller.signal });
} catch (err) {
  if (err instanceof AbortError) {
    console.log('fetch timed out');
  }
}

or shorter:

const { fetch, timeoutSignal, AbortError } = require('@adobe/helix-fetch`);

try {
  await fetch('https://httpbin.org/delay/2', { signal: timeoutSignal(1000) });
} catch (err) {
  if (err instanceof AbortError) {
    console.log('fetch timed out');
  }
}

The existing non-standard timeout option and TimeoutError should be deprecated.

Additional context
Abortable fetch
fetch timeout discussion

@stefan-guggisberg
Copy link
Contributor Author

implemented in bb06b3e

trieloff pushed a commit that referenced this issue Jun 30, 2020
# [1.7.0](v1.6.2...v1.7.0) (2020-06-30)

### Features

* support AbortController for aborting fetch operations ([bb06b3e](bb06b3e)), closes [#64](#64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant