UNPKG

lib-comfoair

Version:

Library to communicate with Zehnder ComfoAirQ ventilation unit through the ComfoControl gateway

34 lines (33 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wait = wait; exports.timeout = timeout; /** * Creates a promise that resolves after a given number of milliseconds. * @param ms The number of milliseconds to wait before resolving the promise. * @returns A promise that resolves after the given number of milliseconds. */ function wait(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } /** * Utility function to add a timeout to a promise, the promise will be rejected with an error if it does not resolve within the given time. * @param promise The promise to add a timeout to. * @param ms The number of milliseconds to wait before rejecting the promise. * @param errorMessage The error message to use when the promise times out. * @returns A promise that resolves when the given promise resolves or rejects when the given promise rejects or times out. */ function timeout(promise, ms, errorMessage) { let timeoutHandle; const timeoutPromise = new Promise((_, reject) => (timeoutHandle = setTimeout(() => reject(new Error(errorMessage)), ms))); const awaitedPromise = promise .then((value) => { clearTimeout(timeoutHandle); return value; }) .catch((error) => { clearTimeout(timeoutHandle); throw error; }); return Promise.race([timeoutPromise, awaitedPromise]); }