From c56d7bdfa4ffb05683aa4feb92b3339247033b89 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Wed, 22 Apr 2026 22:33:10 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Add=20timeout=20handling=20to=20?= =?UTF-8?q?IP=20fetch=20request=20in=20demo.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/demo.ts b/demo.ts index 5916a56..a1ff326 100644 --- a/demo.ts +++ b/demo.ts @@ -33,8 +33,21 @@ export const get_my_ip = tool({ description: "Get my IP", args: {}, async execute(args): Promise { - const ipResp = await fetch("https://hatter.ink/ip/ip.jsonp"); - const ipResponse = await ipResp.json() as IpResponse; - return ipResponse.ip; + const timeoutMs = 10_000; + const abortController = new AbortController(); + const timeoutHandler = setTimeout(() => { + abortController.abort( + `Fetch timeout: ${timeoutMs} ms`, + ); + }, timeoutMs); + try { + const ipResp = await fetch("https://hatter.ink/ip/ip.jsonp", { + signal: abortController.signal, + }); + const ipResponse = await ipResp.json() as IpResponse; + return ipResponse.ip; + } finally { + clearTimeout(timeoutHandler); + } }, });