fetch

Fetches a resource from the network.

Syntax

fetch(request, config?)

Parameters

Return value

A promise that resolves to a Response object.

Rejects a promise on network error. The server response is not considered an error: the promise resolves with a Response object even if the server returns an HTTP error status code.

Example

fetch('https://www.rfc-editor.org/rfc/rfc2616.txt')
  .then(r => r.text())
  .then(s => print(x.split('\n')[23].trim()))
Hypertext Transfer Protocol -- HTTP/1.1

let params = new URLSearchParams({ q: "stars:>1000", sort: "stars" });
let response = await fetch(`https://api.github.com/search/repositories?${params}`)
let results = await response.json()
results.items.slice(0, 4).map(item => item.full_name)
["freeCodeCamp/freeCodeCamp", "EbookFoundation/free-programming-books", "996icu/996.ICU"]
fetch('https://example.com', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ foo: 'bar' })
})

See also

Response