Skip to content

Commit

Permalink
refactor (server-utils): use builtin abort signal timeout (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite authored Oct 30, 2024
1 parent c022c4e commit 265fe65
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions frontend/lib/server-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ interface FetchWithTimeoutOptions extends RequestInit {
export const fetchWithTimeout = async (resource: RequestInfo, options: FetchWithTimeoutOptions = {}): Promise<Response> => {
const { timeout = 10000 } = options;

const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(resource, {
...options,
signal: controller.signal,
});
return response;
} finally {
clearTimeout(id);
}
const response = await fetch(resource, {
...options,
signal: AbortSignal.timeout(timeout),
});
return response;
};

export function extractYouTubeId(url: string): string {
Expand Down

0 comments on commit 265fe65

Please sign in to comment.