fetch è il modo nativo per fare richieste HTTP dal browser. Restituisce una Promise che si risolve in un oggetto Response.
// GET
const res = await fetch("/api/launches");
if (!res.ok) throw new Error("HTTP " + res.status);
const data = await res.json();
// POST
await fetch("/api/launches", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Starship" }),
});
L'insidia numero uno: fetch non rifiuta la Promise sugli errori HTTP (404, 500). Rifiuta solo se la rete cade. Per questo controlli sempre res.ok prima di leggere il corpo.
Ricorda anche che res.json() è a sua volta asincrono (va awaitato). Due await per una risposta: uno per la risposta, uno per il parsing del corpo.