Skip to content

Commit

Permalink
Add fetchWithTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthsky committed May 22, 2024
1 parent 00f9877 commit c82510a
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ <h5>Search movies and TV series</h5>
}
}
return savedAPI = null;


function fetchWithTimeout(url, timeout = 5000) {
return Promise.race([
fetch(url) ,
new Promise((_, reject) =>
setTimeout(() => reject(new Error('Request timed out')), timeout)
)
]);
}
}
</script>

Expand Down Expand Up @@ -207,8 +217,8 @@ <h5>Search movies and TV series</h5>
const encodedSearchText = encodeURIComponent(searchText); // Encode the search text
const apiUrl = `https://www.omdbapi.com/?s=${searchText}&apikey=${apiKey}`;

fetch(apiUrl)
.then(response => response.json())
fetchWithTimeout(apiUrl)

.then(data => {
if (data.Response === "True") {
displayMovies(data.Search);
Expand All @@ -227,6 +237,15 @@ <h5>Search movies and TV series</h5>
});
});

function fetchWithTimeout(url, timeout = 5000) {
return Promise.race([
fetch(url).then(response => response.json()),
new Promise((_, reject) =>
setTimeout(() => reject(new Error('Request timed out')), timeout)
)
]);
}

// Function to display movies
let apiKey;

Expand Down Expand Up @@ -272,8 +291,8 @@ <h5>Search movies and TV series</h5>
// Function to fetch and display seasons for a series
function displaySeasons(imdbID) {
alertZreset()
fetch(`https://www.omdbapi.com/?i=${imdbID}&apikey=${apiKey}`)
.then(response => response.json())
fetchWithTimeout(`https://www.omdbapi.com/?i=${imdbID}&apikey=${apiKey}`, 5000)
.then(data => {
const seasons = data.totalSeasons;
let seasonButtons = '';
Expand Down Expand Up @@ -301,15 +320,15 @@ <h5>Search movies and TV series</h5>
// Function to fetch and display episodes for a specific season
function displayEpisodes(imdbID, seasonNumber) {
alertZreset()
fetch(`https://www.omdbapi.com/?i=${imdbID}&Season=${seasonNumber}&apikey=${apiKey}`)
.then(response => response.json())
fetchWithTimeout(`https://www.omdbapi.com/?i=${imdbID}&Season=${seasonNumber}&apikey=${apiKey}`)
.then(data => {
const episodes = data.Episodes;
let episodePromises = []; // Array to store episode fetch promises

episodes.forEach(episode => {
const promise = fetch(`https://www.omdbapi.com/?i=${episode.imdbID}&apikey=${apiKey}`)
.then(response => response.json())
const promise = fetchWithTimeout(`https://www.omdbapi.com/?i=${episode.imdbID}&apikey=${apiKey}`)
.then(episodeData => episodeData);

episodePromises.push(promise);
Expand Down Expand Up @@ -386,8 +405,8 @@ <h5>Search movies and TV series</h5>
const encodedSearchText = encodeURIComponent(searchText); // Encode the search text
const apiUrl = `https://www.omdbapi.com/?s=${encodedSearchText}&apikey=${apiKey}`;

fetch(apiUrl)
.then(response => response.json())
fetchWithTimeout(apiUrl)
.then(data => {
if (data.Response === "True") {
displayMovies(data.Search);
Expand All @@ -414,8 +433,8 @@ <h5>Search movies and TV series</h5>
const encodedSearchText = encodeURIComponent(searchText); // Encode the search text
const apiUrl = `https://www.omdbapi.com/?s=${encodedSearchText}&apikey=${apiKey}`;

fetch(apiUrl)
.then(response => response.json())
fetchWithTimeout(apiUrl)
.then(data => {
if (data.Response === "True") {
displayMovies(data.Search);
Expand All @@ -442,8 +461,8 @@ <h5>Search movies and TV series</h5>

// Function to fetch movie data based on page number
function fetchMovies(apiUrl, page) {
fetch(apiUrl)
.then(response => response.json())
fetchWithTimeout(apiUrl)
.then(data => {
if (data.Response === "True") {
displayMovies(data.Search);
Expand Down

0 comments on commit c82510a

Please sign in to comment.