UNPKG

@dominicstop/utils

Version:

Yet another event emitter written in typescript.

24 lines (23 loc) 778 B
/** wrapper for timeout that returns a promise */ export 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 */ export 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]); } ;