2021-07-28 21:37:09 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-12-17 21:26:50 +00:00
|
|
|
export type ExplodePromiseResultType<T> = Readonly<{
|
2021-07-28 21:37:09 +00:00
|
|
|
promise: Promise<T>;
|
|
|
|
resolve: (value: T) => void;
|
|
|
|
reject: (error: Error) => void;
|
2021-12-17 21:26:50 +00:00
|
|
|
}>;
|
|
|
|
|
|
|
|
export function explodePromise<T>(): ExplodePromiseResultType<T> {
|
2021-07-28 21:37:09 +00:00
|
|
|
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!,
|
|
|
|
};
|
|
|
|
}
|