kucoin-universal-sdk
Version:
Official KuCoin Universal SDK.
39 lines • 1.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeoutError = void 0;
exports.withTimeout = withTimeout;
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
class TimeoutError extends Error {
constructor(timeoutMs) {
super(`Timeout after ${timeoutMs}ms`);
this.timeoutMs = timeoutMs;
}
}
exports.TimeoutError = TimeoutError;
function withTimeout(executor, timeoutMs) {
return new Promise((outerResolve, outerReject) => {
let handled = false;
const timeout = setTimeout(() => {
if (!handled) {
handled = true;
outerReject(new TimeoutError(timeoutMs));
}
}, timeoutMs);
executor((value) => {
if (!handled) {
handled = true;
clearTimeout(timeout);
outerResolve(value);
}
}, (error) => {
if (!handled) {
handled = true;
clearTimeout(timeout);
outerReject(error);
}
});
});
}
//# sourceMappingURL=util.js.map