fetch()
The global fetch()
method starts the process of fetching a
resource from the network, returning a promise which is fulfilled once the response is
available.
The promise resolves to the Response
object
representing the response to your request.
A fetch()
promise only rejects when a
network error is encountered (which is usually when there's a permissions issue or
similar). A fetch()
promise does
not reject on HTTP errors (404
, etc.). Instead, a
then()
handler must check the Response.ok
and/or
Response.status
properties.
Note: The
fetch()
method's parameters are identical to those of theRequest()
constructor.
Syntax
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 a "toString" method.
- A
Request
object.
options
optional: An object containing any custom settings that you want to apply to the request. The possible options are:
method
- : The request method, e.g.,
GET
,POST
.
- : The request method, e.g.,
headers
- : Any headers you want to add to your request, contained within a
Headers
object or an object literal withString
values.
- : Any headers you want to add to your request, contained within a
body
- : Any body that you want to add to your request: this can be an
ArrayBuffer
, aTypedArray
, aDataView
, aURLSearchParams
, string object or literal, or aReadableStream
object.
- : Any body that you want to add to your request: this can be an
backend
Fastly-specific- Fastly-specific
cacheOverride
Fastly-specificcacheKey
Fastly-specific
Return value
A Promise
that resolves to a Response
object.