use do while

This commit is contained in:
Ashley //// 2024-04-20 19:05:04 +00:00
parent 295c485419
commit fd9a651682

View file

@ -75,19 +75,35 @@ class InnerTubePokeVidious {
* @param {Object} headers - The headers to include in the fetch request.
* @returns {Promise<string>} A promise that resolves to the text content of the response.
*/
async function fetchData(url, headers) {
/**
* @type {Response}
* Fetch data from the specified URL with the given headers, retrying once if status code is 500.
* @param {string} url - The URL to fetch data from.
* @param {Object} headers - The headers to include in the fetch request.
* @returns {Promise<Response>} A promise that resolves to the response object.
*/
const response = await fetch(url, { headers });
async function fetchData(url, headers) {
let response;
let retry = false;
/**
* Retry fetching if status code is 500.
*/
do {
try {
response = await fetch(url, { headers });
if (response.status === 500) {
// If status is 500, fetch again
console.log("Retrying due to status 500...");
return fetchData(url, headers);
retry = true;
} else {
retry = false;
}
} catch (error) {
console.error("Error occurred during fetch:", error);
retry = false;
}
} while (retry);
return response.text();
return response;
}
const { fetch } = await import("undici");