The Fetch API enables web browsers to make HTTP requests to web servers.
😀 There is no longer a need for XMLHttpRequest.
The table lists the first browser versions that fully support the Fetch API.
The example below retrieves a file and shows its content.
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:
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.
async function getText(file) { let myObject = await fetch(file); let myText = await myObject.text(); myDisplay(myText); } |