:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
/**
* Races an operation against a timeout.
* Returns success with the value if the operation completes in time,
* or timeout if the operation takes longer than the specified duration.
*/
export async function withTimeout<T>(
operation: Promise<T>,
timeoutMs: number,
): Promise<{ result: "success"; value: T } | { result: "timeout" }> {
let timeoutHandle!: ReturnType<typeof setTimeout>;
const timeoutPromise = new Promise<{ result: "timeout" }>((resolve) => {
timeoutHandle = setTimeout(() => resolve({ result: "timeout" }), timeoutMs);
});
const operationPromise = operation.then((value) => ({
result: "success" as const,
value,
}));
try {
return await Promise.race([operationPromise, timeoutPromise]);
} finally {
clearTimeout(timeoutHandle);
}
}