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'));
|
|
|
|
});
|
2020-10-13 22:21:42 +00:00
|
|
|
});
|
|
|
|
}
|