UNPKG

@bigmi/core

Version:

TypeScript library for Bitcoin apps.

35 lines 1.15 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