mirror of
https://github.com/bitwarden/browser
synced 2026-02-23 16:13:21 +00:00
* Fix TS errors * sdk loader mock * Downgrade packages to be CommonJS-compatible * Fix formattinmg * Move logs to service * package lock fixes
30 lines
714 B
TypeScript
30 lines
714 B
TypeScript
// FIXME: Update this file to be type safe and remove this and next line
|
|
// @ts-strict-ignore
|
|
export const race = <T>({
|
|
promise,
|
|
timeout,
|
|
error,
|
|
}: {
|
|
promise: Promise<T>;
|
|
timeout: number;
|
|
error?: Error;
|
|
}): Promise<T> => {
|
|
let timer: NodeJS.Timeout | null = 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) => {
|
|
if (timer != null) {
|
|
clearTimeout(timer);
|
|
}
|
|
return value;
|
|
}),
|
|
]);
|
|
};
|