DrSchool Hızlı kod öğren.
EN AR BN CS DE EL Ara

JS: Fetch API

JS içinde Fetch API konusunu örneklerle öğren.

Okuma süresi: 1 min Kelime: 35
Playground'u aç → HTML İlerlemem

JS — Fetch API

Genel Bakış

Fetch ile API’lerden veri (JSON/metin) alırsın.

Öğreneceklerin

  • Temel fetch
  • JSON okuma
  • Hata yönetimi
  • async/await

Sözdizimi

const res = await fetch(url);

Örnek

async function load(){
  try{
    const res = await fetch("https://api.github.com/zen");
    if(!res.ok) throw new Error("HTTP " + res.status);
    const text = await res.text();
    console.log(text);
  }catch(err){
    console.error(err);
  }
}
load();

Notlar

  • res.ok kontrol et.
  • async/await ile try/catch kullan.

    Kendin Dene: Playground aç: /tr/playground/html