signal-desktop/ts/util/explodePromise.ts

28 lines
790 B
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export type ExplodePromiseResultType<T> = Readonly<{
promise: Promise<T>;
resolve: (value: T) => void;
reject: (error: Error) => void;
}>;
export function explodePromise<T>(): ExplodePromiseResultType<T> {
let resolve: (value: T) => void;
let reject: (error: Error) => void;
const promise = new Promise<T>((innerResolve, innerReject) => {
resolve = innerResolve;
reject = innerReject;
});
return {
promise,
// Typescript thinks that resolve and reject can be undefined here.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
resolve: resolve!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
reject: reject!,
};
}