fix: Anify timeout logic running even when fetch resolved

Summary:

Test Plan:
This commit is contained in:
2024-05-26 13:22:03 -04:00
parent 00ff0e0295
commit 6e8fe4f7b0
3 changed files with 64 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
import { PromiseTimedOutError, promiseTimeout } from "~/libs/promiseTimeout";
import { sortByProperty } from "~/libs/sortByProperty";
import type { EpisodesResponse } from "./episode";
@@ -11,22 +12,18 @@ export async function getEpisodesFromAnify(
}
let response: AnifyEpisodesResponse[] | null = null;
const abortController = new AbortController();
try {
const abortController = new AbortController();
response = await Promise.race([
response = await promiseTimeout(
fetch(`https://api.anify.tv/episodes/${aniListId}`, {
signal: abortController.signal,
}).then((res) => res.json<AnifyEpisodesResponse[]>()),
// set a limit of 30 seconds
new Promise((resolve) => setTimeout(resolve, 30 * 1000)).then(() => {
abortController.abort("Loading episodes from Anify timed out");
console.error(
`Loading episodes from Anify timed out; aniListId: ${aniListId}`,
);
return null;
}),
]);
30 * 1000,
);
} catch (e) {
if (e instanceof PromiseTimedOutError) {
abortController.abort("Loading episodes from Anify timed out");
}
console.error(
new Error(
`Error trying to load episodes from anify; aniListId: ${aniListId}`,