Redi School Munich - Spring 2021
▶ Demo
fetch
MDN: Fetch API
Syntax:
fetch('https://some-url.com')
.then(response => {
// we can work with the API response now.
});
fetch('https://randomuser.me/api/?nat=us&randomapi')
.then(response => response.json())
.then(data => {
const person = data.results[0];
alert('Got a random user named ' + person.name.first + ' ' + person.name.last);
});
.then
?
fetch('https://google.com')
.then(response => {
console.log('Got response');
});
console.log('After fetch');
Output:
After fetch
Got response
This syntax is called Promise.