Skip to main content
Glama
withTimeout.js1.16 kB
/** * Utility function to add timeout to async operations * Prevents hanging requests by throwing an error if operation takes too long */ /** * Wraps a promise with a timeout * @param {Promise} promise - The promise to wrap * @param {number} timeoutMs - Timeout in milliseconds * @param {string} operationName - Name of the operation for error messages * @returns {Promise} - The wrapped promise that will reject if timeout is exceeded */ export async function withTimeout(promise, timeoutMs, operationName = 'Operation') { let timeoutId; const timeoutPromise = new Promise((_, reject) => { timeoutId = setTimeout(() => { reject(new Error(`${operationName} timed out after ${timeoutMs}ms`)); }, timeoutMs); }); try { const result = await Promise.race([promise, timeoutPromise]); clearTimeout(timeoutId); return result; } catch (error) { clearTimeout(timeoutId); throw error; } } /** * Default timeout for CCXT operations (15 seconds) * This is lower than the global server timeout to ensure we can * return a meaningful error before the server times out */ export const DEFAULT_CCXT_TIMEOUT = 15000;

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/carlosatta/mcp-server-ccxt'

If you have feedback or need assistance with the MCP directory API, please join our Discord server