Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

980 B
Raw Blame
export async function withTimeout(promise, timeoutMs, labelOrOptions = {
    message: "timeout",
}) {
    if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
        return await promise;
    }
    const options = typeof labelOrOptions === "string" ? { label: labelOrOptions } : labelOrOptions;
    const createError = options.createError ??
        (() => new Error(options.message ?? `${options.label ?? "operation"} timed out after ${timeoutMs}ms`));
    let timeoutId;
    try {
        return await Promise.race([
            promise,
            new Promise((_, reject) => {
                timeoutId = setTimeout(() => {
                    try {
                        reject(createError());
                    }
                    catch (error) {
                        reject(error);
                    }
                }, timeoutMs);
            }),
        ]);
    }
    finally {
        if (timeoutId) {
            clearTimeout(timeoutId);
        }
    }
}