APIs e Fetch
Consumo de dados externos com fluxo assincorno previsivel.
O que e API
API e contrato de comunicacao entre cliente e servidor.
JSON
{
"nome": "Leonardo",
"skills": ["html", "css", "js"]
}Fetch basico
fetch('/api/users')
.then(r => r.json())
.then(data => console.log(data));Async e await
async function loadUsers() {
const res = await fetch('/api/users');
return await res.json();
}Tratamento de erros
try {
const res = await fetch('/api/data');
if (!res.ok) throw new Error('Falha na requisicao');
} catch (err) {
console.error(err);
}