Middle
/** * Bir URL'yi giriş olarak alan ve * bu URL'ye GET isteği yapan ve verileri (json) döndüren bir fonksiyon yazmak gerekir. * Verileri almak için fetch kullanın. * Sadece Promise API kullanılabilir. * İstek sırasında bir hata olursa, 5 kez daha dene. * Sonuçta bilgiyi alamazsan, "Belirtilen URL erişilebilir değil" hatasını döndür. */ function get(url) { // kod buraya } get(url) .then(res => console.log(res)) .catch(err => console.error(err))
sobes.tech yapay zeka
AI'dan gelen yanıt
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));