mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-04 18:37:45 +01:00
26 lines
548 B
TypeScript
26 lines
548 B
TypeScript
|
export const race = <T>({
|
||
|
promise,
|
||
|
timeout,
|
||
|
error,
|
||
|
}: {
|
||
|
promise: Promise<T>;
|
||
|
timeout: number;
|
||
|
error?: Error;
|
||
|
}) => {
|
||
|
let timer = null;
|
||
|
|
||
|
// Similar to Promise.all, but instead of waiting for all, it resolves once one promise finishes.
|
||
|
// Using this so we can reject if the timeout threshold is hit
|
||
|
return Promise.race([
|
||
|
new Promise<T>((_, reject) => {
|
||
|
timer = setTimeout(reject, timeout, error);
|
||
|
return timer;
|
||
|
}),
|
||
|
|
||
|
promise.then((value) => {
|
||
|
clearTimeout(timer);
|
||
|
return value;
|
||
|
}),
|
||
|
]);
|
||
|
};
|