-
Notifications
You must be signed in to change notification settings - Fork 22.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Request.signal * Add entry in Request interface * Fix 2 typos * Update files/en-us/web/api/request/signal/index.md Co-authored-by: dawei-wang <dawei-wang@users.noreply.github.com> * Update files/en-us/web/api/request/signal/index.md Co-authored-by: dawei-wang <dawei-wang@users.noreply.github.com> * Update files/en-us/web/api/request/signal/index.md Co-authored-by: Joshua Chen <sidachen2003@gmail.com> --------- Co-authored-by: dawei-wang <dawei-wang@users.noreply.github.com> Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
- Loading branch information
1 parent
12b18a5
commit 12ac0a3
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: Request.signal | ||
slug: Web/API/Request/signal | ||
page-type: web-api-instance-property | ||
browser-compat: api.Request.signal | ||
--- | ||
|
||
{{APIRef("Fetch API")}} | ||
|
||
The read-only **`signal`** property of the {{DOMxRef("Request")}} interface returns the {{domxref("AbortSignal")}} associated with the request. | ||
|
||
## Value | ||
|
||
An {{DOMxRef("AbortSignal")}} object. | ||
|
||
## Examples | ||
|
||
```js | ||
// Create a new abort controller | ||
const controller = new AbortController(); | ||
|
||
// Create a request with this controller's AbortSignal object | ||
const req = new Request('/', { signal: controller.signal }); | ||
|
||
// Add an event handler logging a message in case of abort | ||
req.signal.addEventListener("signal", () => { | ||
console.log("abort"); | ||
}); | ||
|
||
// In case of abort, log the AbortSignal reason, if any | ||
fetch(req).catch(() => { | ||
if (signal.aborted) { | ||
if (signal.reason) { | ||
console.log(`Request aborted with reason: ${signal.reason}`); | ||
} else { | ||
console.log("Request aborted but no reason was given."); | ||
} | ||
} else { | ||
console.log("Request not aborted, but terminated abnormally."); | ||
} | ||
}; | ||
|
||
// Actually abort the request | ||
controller.abort(); | ||
``` | ||
## Specifications | ||
{{Specifications}} | ||
## Browser compatibility | ||
{{Compat}} |