signal-desktop/ts/util/sleep.ts

17 lines
483 B
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2022-10-18 17:12:02 +00:00
export function sleep(ms: number, abortSignal?: AbortSignal): Promise<void> {
return new Promise((resolve, reject) => {
let timeout: NodeJS.Timeout | undefined = setTimeout(resolve, ms);
abortSignal?.addEventListener('abort', () => {
if (timeout !== undefined) {
clearTimeout(timeout);
timeout = undefined;
}
reject(new Error('Aborted'));
});
});
}