Untitled

 avatar
unknown
plain_text
10 months ago
1.0 kB
14
Indexable
// Sahte JSON verisi (API'den geliyormuş gibi)
const fakePosts = [
  { id: 1, title: "Post 1: JavaScript'e giriş" },
  { id: 2, title: "Post 2: Async/Await nasıl çalışır?" },
  { id: 3, title: "Post 3: React Native Başlangıç" },
  { id: 4, title: "Post 4: Node.js ile Backend" },
  { id: 5, title: "Post 5: Frontend - Backend Bağlantısı" },
  { id: 6, title: "Post 6: Mobil Programlama Ödevi" }
];

// Sahte API fonksiyonu
function fetchPosts() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(fakePosts);
    }, 1000); // 1 saniye bekletiyoruz, gerçek API gibi
  });
}

// Async/Await ile ilk 5 post'un başlıklarını yazdırma
async function getFirstFivePosts() {
  try {
    const posts = await fetchPosts(); // "API çağrısı"
    console.log("İlk 5 post başlığı:");
    posts.slice(0, 5).forEach((post) => console.log(post.title));
  } catch (error) {
    console.error("Hata oluştu:", error);
  }
}

// Fonksiyonu çalıştır
getFirstFivePosts();
Editor is loading...
Leave a Comment