Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Web Fetch API

The Fetch API enables web browsers to make HTTP requests to web servers.

😀 There is no longer a need for XMLHttpRequest.

Browser Support

The table lists the first browser versions that fully support the Fetch API.

DOM 3

A Fetch API Example

The example below retrieves a file and shows its content.

Example

fetch(file)
.then(x => x.text())
.then(y => myDisplay(y));

Since Fetch uses async and await, the example above might be clearer when written as follows:

Example

async function getText(file) {
  let x = await fetch(file);
  let y = await x.text();
  myDisplay(y);
}

Or even better: Use more descriptive names instead of x and y.

Example

async function getText(file) {
  let myObject = await fetch(file);
  let myText = await myObject.text();
  myDisplay(myText);
}