Skip to content

Commit

Permalink
Document 2 more exceptions; explain about aborting requests after fet…
Browse files Browse the repository at this point in the history
…ch fulfills
  • Loading branch information
wbamberg committed Jul 3, 2024
1 parent fdf83db commit 5577273
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 28 deletions.
36 changes: 27 additions & 9 deletions files/en-us/web/api/abortcontroller/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ We first create a controller using the {{domxref("AbortController.AbortControlle

When the [fetch request](/en-US/docs/Web/API/fetch) is initiated, we pass in the `AbortSignal` as an option inside the request's options object (the `{signal}` below). This associates the signal and controller with the fetch request and allows us to abort it by calling {{domxref("AbortController.abort()")}}, as seen below in the second event listener.

When `abort()` is called, the `fetch()` promise rejects with a `DOMException` named `AbortError`.

```js
let controller;
const url = "video.mp4";
Expand All @@ -52,20 +54,36 @@ abortBtn.addEventListener("click", () => {
}
});

function fetchVideo() {
async function fetchVideo() {
controller = new AbortController();
const signal = controller.signal;
fetch(url, { signal })
.then((response) => {
console.log("Download complete", response);
})
.catch((err) => {
console.error(`Download error: ${err.message}`);
});

try {
const response = await fetch(url, { signal });
console.log("Download complete", response);
// process response further
} catch (err) {
console.error(`Download error: ${err.message}`);
}
}
```

> **Note:** When `abort()` is called, the `fetch()` promise rejects with a `DOMException` named `AbortError`.
If the request is aborted after the `fetch()` call has been fulfilled but before the response body has been read, then attempting to read the response body will reject with an `AbortError` exception.

```js
async function get() {
const controller = new AbortController();
const request = new Request("https://example.org/get", {
signal: controller.signal,
});

const response = await fetch(request);
controller.abort();
// The next line will throw `AbortError`
const text = await response.text();
console.log(text);
}
```

You can find a [full working example on GitHub](https://github.com/mdn/dom-examples/tree/main/abort-api); you can also see it [running live](https://mdn.github.io/dom-examples/abort-api/).

Expand Down
49 changes: 35 additions & 14 deletions files/en-us/web/api/abortsignal/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,54 @@ We first create an abort controller using the {{domxref("AbortController.AbortCo
When the [fetch request](/en-US/docs/Web/API/fetch) is initiated, we pass in the `AbortSignal` as an option inside the request's options object (the `{signal}` below). This associates the signal and controller with the fetch request, and allows us to abort it by calling {{domxref("AbortController.abort()")}}.
Below you can see that the fetch operation is aborted in the second event listener, which triggered when the abort button (`abortBtn`) is clicked.

```js
const controller = new AbortController();
const signal = controller.signal;
When `abort()` is called, the `fetch()` promise rejects with a `DOMException` named `AbortError`.

```js
let controller;
const url = "video.mp4";

const downloadBtn = document.querySelector(".download");
const abortBtn = document.querySelector(".abort");

downloadBtn.addEventListener("click", fetchVideo);

abortBtn.addEventListener("click", () => {
controller.abort();
console.log("Download aborted");
if (controller) {
controller.abort();
console.log("Download aborted");
}
});

function fetchVideo() {
fetch(url, { signal })
.then((response) => {
console.log("Download complete", response);
})
.catch((err) => {
console.error(`Download error: ${err.message}`);
});
async function fetchVideo() {
controller = new AbortController();
const signal = controller.signal;

try {
const response = await fetch(url, { signal });
console.log("Download complete", response);
// process response further
} catch (err) {
console.error(`Download error: ${err.message}`);
}
}
```

> **Note:** When `abort()` is called, the `fetch()` promise rejects with an "`AbortError`" `DOMException`.
If the request is aborted after the `fetch()` call has been fulfilled but before the response body has been read, then attempting to read the response body will reject with an `AbortError` exception.

```js
async function get() {
const controller = new AbortController();
const request = new Request("https://example.org/get", {
signal: controller.signal,
});

const response = await fetch(request);
controller.abort();
// The next line will throw `AbortError`
const text = await response.text();
console.log(text);
}
```

You can find a [full working example on GitHub](https://github.com/mdn/dom-examples/tree/main/abort-api); you can also see it [running live](https://mdn.github.io/dom-examples/abort-api/).

Expand Down
17 changes: 17 additions & 0 deletions files/en-us/web/api/fetch_api/using_fetch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,23 @@ cancelButton.addEventListener("click", () => {
});
```

If the request is aborted after the `fetch()` call has been fulfilled but before the response body has been read, then attempting to read the response body will reject with an `AbortError` exception.

```js
async function get() {
const controller = new AbortController();
const request = new Request("https://example.org/get", {
signal: controller.signal,
});

const response = await fetch(request);
controller.abort();
// The next line will throw `AbortError`
const text = await response.text();
console.log(text);
}
```

## Handling the response

As soon as the browser has received the response status and headers from the server (and potentially before the response body itself has been received), the promise returned by `fetch()` is fulfilled with a {{domxref("Response")}} object.
Expand Down
6 changes: 5 additions & 1 deletion files/en-us/web/api/response/arraybuffer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ A promise that resolves with an {{jsxref("ArrayBuffer")}}.

### Exceptions

- {{domxref("DOMException")}} `AbortError`
- : The request was [aborted](/en-US/docs/Web/API/Fetch_API/Using_Fetch#canceling_a_request).
- {{jsxref("TypeError")}}
- : The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- : Thrown for one of the following reasons:
- The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- There is an error decoding the body content (for example, because the {{httpheader("Content-Encoding")}} header is incorrect).
- {{jsxref("RangeError")}}
- : There is a problem creating the associated `ArrayBuffer`.
For example, if the data size is more than [`Number.MAX_SAFE_INTEGER`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER).
Expand Down
4 changes: 3 additions & 1 deletion files/en-us/web/api/response/blob/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ A promise that resolves with a {{domxref("Blob")}}.
### Exceptions

- {{jsxref("TypeError")}}
- : The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- : Thrown for one of the following reasons:
- The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- There is an error decoding the body content (for example, because the {{httpheader("Content-Encoding")}} header is incorrect).

## Examples

Expand Down
4 changes: 3 additions & 1 deletion files/en-us/web/api/response/bytes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ A promise that resolves with an {{jsxref("Uint8Array")}}.
### Exceptions

- {{jsxref("TypeError")}}
- : The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- : Thrown for one of the following reasons:
- The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- There is an error decoding the body content (for example, because the {{httpheader("Content-Encoding")}} header is incorrect).
- {{jsxref("RangeError")}}
- : There is a problem creating the associated `ArrayBuffer`.
For example, if the data size is more than [`Number.MAX_SAFE_INTEGER`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER).
Expand Down
1 change: 1 addition & 0 deletions files/en-us/web/api/response/formdata/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ A {{jsxref("Promise")}} that resolves with a {{domxref("FormData")}} object.
- {{jsxref("TypeError")}}
- : Thrown for one of the following reasons:
- The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- There is an error decoding the body content (for example, because the {{httpheader("Content-Encoding")}} header is incorrect).
- The {{glossary("MIME")}} type of the body cannot be determined from the {{httpheader("Content-Type")}} headers included in the response.
- The body cannot be parsed as a `FormData` object.

Expand Down
4 changes: 3 additions & 1 deletion files/en-us/web/api/response/json/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ anything that can be represented by JSON — an object, an array, a string, a nu
### Exceptions

- {{jsxref("TypeError")}}
- : The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- : Thrown for one of the following reasons:
- The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- There is an error decoding the body content (for example, because the {{httpheader("Content-Encoding")}} header is incorrect).
- {{jsxref("SyntaxError")}}
- : The response body cannot be parsed as JSON.

Expand Down
4 changes: 3 additions & 1 deletion files/en-us/web/api/response/text/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ A Promise that resolves with a {{jsxref("String")}}.
### Exceptions

- {{jsxref("TypeError")}}
- : The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- : Thrown for one of the following reasons:
- The response body is [disturbed or locked](/en-US/docs/Web/API/Fetch_API/Using_Fetch#locked_and_disturbed_streams).
- There is an error decoding the body content (for example, because the {{httpheader("Content-Encoding")}} header is incorrect).

## Examples

Expand Down

0 comments on commit 5577273

Please sign in to comment.