Modern sites exchange data with servers: weather, news, products and so on. Those exchanges go through APIs, and JavaScript queries them with fetch.
What is an API?
An API is a service that supplies data on request. You send a request to an address and it sends back a response, usually in JSON format (structured text that code can read easily).
Using fetch
fetch("https://api.exemple.com/donnees")
.then(reponse => reponse.json())
.then(donnees => console.log(donnees));
Here is what happens, step by step:
fetchsends the request to the address you give it..json()turns the response into usable data.- The second
.thenfinally receives that data so you can work with it.
Because the response can take a while, these operations are asynchronous: the code does not wait around and handles the result as soon as it arrives.
Key takeaway:
fetchretrieves remote data through an API. The response comes back later: you handle it with.then.