درس 4 من 8
Fetch API في JavaScript
يوفر المتصفح الدالة fetch() لإرسال HTTP requests. تعيد Promise، لذلك تستخدم .then() أو async/await للتعامل مع الاستجابة.
APIS
// استخدام fetch مع promises
fetch("https://api.example.com/users")
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
// استخدام async/await: الطريقة الحديثة
async function getUsers() {
const response = await fetch("https://api.example.com/users");
const data = await response.json();
console.log(data);
}