-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict' | ||
|
||
/** | ||
* @type {<T = void, R = T>(time: number, options?: { promise?: Promise<T>; fallback?: () => R | Promise<R>; signal?: AbortSignal; rejection?: boolean; }) => Promise<T | Awaited<R>>} | ||
*/ | ||
async function timeout (time, options = {}) { | ||
let timer | ||
/** @type {() => void} */ | ||
let abort | ||
const { promise, fallback, signal, rejection } = options | ||
if (signal?.aborted) { | ||
throw new DOMException('The operation was aborted.', 'AbortError') | ||
} | ||
const delay = new Promise((resolve, reject) => { | ||
abort = () => { | ||
clearTimeout(timer) | ||
reject( | ||
signal?.reason ?? | ||
new DOMException('The operation was aborted.', 'AbortError') | ||
) | ||
} | ||
signal?.addEventListener('abort', abort) | ||
timer = setTimeout(() => { | ||
if (typeof fallback === 'function') { | ||
try { | ||
resolve(fallback()) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
} else { | ||
if (rejection === false) { | ||
resolve(undefined) | ||
} else { | ||
reject(new DOMException('The operation was aborted.', 'AbortError')) | ||
} | ||
} | ||
}, time) | ||
}) | ||
try { | ||
return promise ? await Promise.race([promise, delay]) : await delay | ||
} finally { | ||
clearTimeout(timer) | ||
signal?.removeEventListener('abort', abort) | ||
} | ||
} | ||
|
||
/** | ||
* @type {<T, R = T>(promise: Promise<T>, time: number, fallback?: () => R | Promise<R>) => Promise<Awaited<R> | T>} | ||
*/ | ||
function pTimeout (promise, time, fallback) { | ||
return timeout(time, { promise, fallback }) | ||
} | ||
|
||
/** | ||
* @param {number} time | ||
*/ | ||
function delay (time) { | ||
return timeout(time, { rejection: false }) | ||
} | ||
|
||
module.exports = { | ||
timeout, | ||
delay, | ||
pTimeout | ||
} |