Guide / JavaScript — basi del linguaggio / Promise e async/await

Promise e async/await

Asincronia: then/catch e sintassi async.

Una Promise rappresenta un valore futuro; .then e .catch concatenano operazioni. async function restituisce sempre una Promise; await sospende solo dentro async.

Usa try/catch con await per errori come nel codice sincrono.

Esempio

async function carica() {
    const res = await fetch("/api/dati");
    if (!res.ok) throw new Error("HTTP " + res.status);
    return res.json();
}