refactor(probe): share withTimeout

This commit is contained in:
Peter Steinberger
2026-02-16 00:39:11 +00:00
parent 5ecc364d55
commit 32221e194a
3 changed files with 16 additions and 30 deletions

View File

@@ -1,5 +1,6 @@
import { messagingApi } from "@line/bot-sdk";
import type { LineProbeResult } from "./types.js";
import { withTimeout } from "../utils/with-timeout.js";
export async function probeLineBot(
channelAccessToken: string,
@@ -30,18 +31,3 @@ export async function probeLineBot(
return { ok: false, error: message };
}
}
function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
if (!timeoutMs || timeoutMs <= 0) {
return promise;
}
let timer: NodeJS.Timeout | null = null;
const timeout = new Promise<T>((_, reject) => {
timer = setTimeout(() => reject(new Error("timeout")), timeoutMs);
});
return Promise.race([promise, timeout]).finally(() => {
if (timer) {
clearTimeout(timer);
}
});
}