UNPKG

@dominicstop/utils

Version:

Yet another event emitter written in typescript.

28 lines (27 loc) 917 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.timeout = timeout; exports.promiseWithTimeout = promiseWithTimeout; /** wrapper for timeout that returns a promise */ function timeout(ms) { return new Promise(resolve => { const timeoutID = setTimeout(() => { clearTimeout(timeoutID); resolve(); }, ms); }); } ; /** Wraps a promise that will reject if not not resolved in <ms> milliseconds */ function promiseWithTimeout(ms, promise) { // Create a promise that rejects in <ms> milliseconds const timeoutPromise = new Promise((_, reject) => { const timeoutID = setTimeout(() => { clearTimeout(timeoutID); reject(`Promise timed out in ${ms} ms.`); }, ms); }); // Returns a race between our timeout and the passed in promise return Promise.race([promise, timeoutPromise]); } ;