Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. WorkerGlobalScope
  4. fetch()

WorkerGlobalScope: fetch() method

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

* Some parts of this feature may have varying levels of support.

Note: This feature is only available inWeb Workers.

Thefetch() method of theWorkerGlobalScope interface starts the process of fetching a resource from the network, returning a promise that is fulfilled once the response is available.

The promise resolves to theResponse object representing the response to your request.

Afetch() promise only rejects when the request fails, for example, because of a badly-formed request URL or a network error.Afetch() promisedoes not reject if the server responds with HTTP status codes that indicate errors (404,504, etc.).Instead, athen() handler must check theResponse.ok and/orResponse.status properties.

Thefetch() method is controlled by theconnect-src directive ofContent Security Policy rather than the directive of the resources it's retrieving.

Note:Thefetch() method's parameters are identical to those of theRequest() constructor.

Syntax

js
fetch(resource)fetch(resource, options)

Parameters

resource

This defines the resource that you wish to fetch. This can either be:

  • A string or any other object with astringifier — including aURL object — that provides the URL of the resource you want to fetch. The URL may be relative to the base URL, which is the document'sbaseURI in a window context, orWorkerGlobalScope.location in a worker context.
  • ARequest object.
optionsOptional

ARequestInit object containing any custom settings that you want to apply to the request.

Return value

APromise that resolves to aResponse object.

Exceptions

AbortErrorDOMException

The request was aborted due to a call to theAbortControllerabort() method.

NotAllowedErrorDOMException

Thrown if use of theTopics API is specifically disallowed by abrowsing-topicsPermissions Policy, and afetch() request was made withbrowsingTopics: true.

TypeError

An error when the fetch operation could not be performed.SeeWindow.fetch() exceptions for a list of reasons why this error can occur.

Examples

Seefetch() for examples.

Specifications

Specification
Fetch
# fetch-method

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp