fix: Anify timeout logic running even when fetch resolved
Summary: Test Plan:
This commit is contained in:
@@ -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}`,
|
||||
|
||||
Reference in New Issue
Block a user