Middle
/** * Giriş olaraq URL qəbul edən və * bu URL-ə GET sorğusu göndərən və məlumatları (json) qaytaran funksiya yazmaq lazımdır. * Məlumatları əldə etmək üçün fetch istifadə edin. * Yalnız Promise API istifadə oluna bilər. * Sorğu zamanı xəta baş verərsə, 5 dəfə yenidən cəhd edin. * Əgər sonunda məlumat əldə etmək mümkün deyilsə, "Təyin olunan URL əlçatan deyil" xətasını qaytarın. */ function get(url) { // kod burada } get(url) .then(res => console.log(res)) .catch(err => console.error(err))
sobes.tech Süni İntellekt
AI-dan cavab
javascript function get(url) { const maxRetries = 5; let attempts = 0;
function tryFetch() {
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.catch(error => {
if (attempts < maxRetries) {
attempts++;
return tryFetch();
} else {
return Promise.reject(new Error('Заданный URL недоступен'));
}
});
}
return tryFetch();
}
// Пример использования: // get('https://api.example.com/data') // .then(res => console.log(res)) // .catch(err => console.error(err));