Almost every real product loads data from a server: account balances, order history, product lists. Working with REST APIs is one of the skills that appears most often in the adverts we studied. On Ghanaian mobile networks, handling slow and dropped connections well is just as important as getting the data.
What an API is
An API is a set of addresses on a server that return data instead of web pages, usually as JSON. JSON looks like JavaScript objects:
{ "item": "rice", "price": 20, "inStock": true }Asynchronous code
Network requests take time. JavaScript does not freeze while it waits; it carries on and comes back when the data arrives. This is called asynchronous code. The modern way to write it uses async and await:
async function loadPrices() {
const res = await fetch("/api/prices");
if (!res.ok) throw new Error("Server said " + res.status);
return res.json();
}await pauses this function, not the whole page, until the response arrives. is true for success codes such as 200. only throws on network failure, so you must check yourself for errors like 404 or 500.