DEV Community

Madhan Raj
Madhan Raj

Posted on

Javascript Fetch API

JavaScript Fetch API
Examples

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

Enter fullscreen mode Exit fullscreen mode

Fetch is based on async and await. The example might be easier to understand like this:

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

Use understandable names instead of x and y:

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

Description

The fetch() method starts the process of fetching a resource from a server.

The fetch() method returns a Promise that resolves to a Response object.

Syntax
fetch(file)

Parameters
Parameter | Description
file | Optional.
The name of a resource to fetch.
Return Value

Type | Description
Promise | A Promise that resolves to a Response object.

Reference
https://www.w3schools.com/jsref/api_fetch.asp

Top comments (1)

Collapse
 
raja_b_0c9d242e2c26cf063b profile image
Raja B

Mr.Madhan

What is the Fetch API?

What is a Response object?