Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

fetch — asynchronous resource retrieval

fetch() retrieves a resource and returns Promise<Response>. An HTTP error status does not automatically reject the promise; check response.ok.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

fetch(input, init?) → Promise<Response>

fetch(input, init?) is a Web API, not part of ECMAScript. It accepts a URL or Request; optional init configures the request. If init.method is not specified, a URL input defaults to GET, while an existing Request keeps its method (for example, a POST request stays POST).

await fetch(...) provides a Response, not its consumed body. await response.json() reads and parses JSON; it can reject. The body can generally be read only once.

response.ok means status 200–299. HTTP 404 and 500 alone do not reject the promise; network errors do. Browsers enforce CORS: mode: 'no-cors' does not unlock cross-origin response reading.

Examples

async function main() {
  const response = await fetch('data:application/json,%7B%22value%22%3A42%7D');
  if (!response.ok) {
    throw new Error(`HTTP ${response.status}`);
  }
  const data = await response.json();
  console.log(data.value);
}

main().catch(console.error);

Result

42

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources