← Trail· Lesson 6 of 7
Introduction to fetch
Fetch async data with fetch, async/await, and JSON.
TODO i18n: Full body translation pending. Title and description are localized; the body below remains in Portuguese for now.
fetch busca recursos pela rede (ou URLs locais) e devolve uma Promise.
async function carregar() {
const res = await fetch('/dados.json');
if (!res.ok) throw new Error('Falha na rede');
const dados = await res.json();
return dados;
}
No playground usamos dados mock em memória — a API é a mesma. Sempre trate erro e estado de carregamento na UI.