UNPKG

viem

Version:

TypeScript Interface for Ethereum

34 lines 1.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withTimeout = withTimeout; function withTimeout(fn, { errorInstance = new Error('timed out'), timeout, signal, }) { return new Promise((resolve, reject) => { ; (async () => { let timeoutId; try { const controller = new AbortController(); if (timeout > 0) { timeoutId = setTimeout(() => { if (signal) { controller.abort(); } else { reject(errorInstance); } }, timeout); } resolve(await fn({ signal: controller?.signal || null })); } catch (err) { if (err?.name === 'AbortError') reject(errorInstance); reject(err); } finally { clearTimeout(timeoutId); } })(); }); } //# sourceMappingURL=withTimeout.js.map