Skip to main content

fetch

Fetch a resource from the network, and return a promise with the response.

Signature​

fetch(url: string, init?: FetchOptions): Promise<FetchResponse>

Parameters​

url​

The URL of the requested resource. Unlike standardized fetch, this must be a string.

init​

An optional argument with the following optional parameters:

interface FetchOptions {
method?: string
headers?: {[name: string]: string}
body?: Uint8Array | string
credentials?: string
cache?: string
redirect?: string
referrer?: string
integrity?: string
}

Calling fetch returns a FetchResponse object:

interface FetchResponse {
headersObject: {[name: string]: string}
ok: boolean
redirected: boolean
status: number
statusText: string
type: string
url: string
arrayBuffer(): Promise<ArrayBuffer>
text(): Promise<string>
json(): Promise<any>
}
  • headersObject: The headers associated with the response. Note that unlike the standardized fetch, this is a plain object.
  • ok: Whether the result was successful.
  • redirected: Whether the response is the result of a redirect.
  • status: The status code of the response.
  • statusText: The status text corresponding to this status code.
  • type: The type of response.
  • url: The URL of the response.
  • arrayBuffer(): Returns a promise with the contents of the response body as a ArrayBuffer.
  • text(): Returns a promise with the contents of the response body as a string, decoded as utf-8.
  • json(): Returns a promise with the contents of the response body as Javascript object.

Remarks​

This function has similar behavior to the standardized fetch() function, with some minor differences. See the FetchOptions and FetchResponse interfaces for more information.