Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions files/en-us/web/api/abortcontroller/abort/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,32 @@ We first create a controller using the {{domxref("AbortController.AbortControlle
When the [fetch request](/en-US/docs/Web/API/Window/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 `AbortController.abort()`, as seen below in the second event listener.

```js
const controller = new AbortController();
const signal = controller.signal;

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}`);
}
}
```

Expand Down
30 changes: 17 additions & 13 deletions files/en-us/web/api/abortcontroller/abortcontroller/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,32 @@ We first create a controller using the `AbortController()` constructor, then gra
When the [fetch request](/en-US/docs/Web/API/Window/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.

```js
const controller = new AbortController();
const signal = controller.signal;

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}`);
}
}
```

Expand Down
30 changes: 17 additions & 13 deletions files/en-us/web/api/abortcontroller/signal/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,32 @@ We first create a controller using the {{domxref("AbortController.AbortControlle
When the [fetch request](/en-US/docs/Web/API/Window/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.

```js
const controller = new AbortController();
const signal = controller.signal;

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}`);
}
}
```

Expand Down