add throwIfAborted and rejectIfAborted utilities to simplify AbortSignal handling
This commit is contained in:
parent
8d848703b5
commit
5f37eef244
1 changed files with 20 additions and 0 deletions
20
packages/backend/src/misc/throw-if-aborted.ts
Normal file
20
packages/backend/src/misc/throw-if-aborted.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { AbortedError } from '@/misc/errors/AbortedError.js';
|
||||||
|
|
||||||
|
export function throwIfAborted(signal: AbortSignal): void {
|
||||||
|
if (signal.aborted) {
|
||||||
|
throw new AbortedError(signal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function rejectIfAborted(signal: AbortSignal): Promise<void> {
|
||||||
|
if (signal.aborted) {
|
||||||
|
return Promise.reject(new AbortedError(signal));
|
||||||
|
} else {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue