aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/utilities/promise.ts
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-09-21 21:05:04 +0800
committercrupest <crupest@outlook.com>2023-09-21 21:05:04 +0800
commita8a8385cd959e4d9d57b8a35381d2851049c07ff (patch)
treea933e8a6c1e8b577149dba624514148507a61a50 /FrontEnd/src/utilities/promise.ts
parent04863bd8ebb543a436bde0d49250010038ab9f90 (diff)
downloadtimeline-a8a8385cd959e4d9d57b8a35381d2851049c07ff.tar.gz
timeline-a8a8385cd959e4d9d57b8a35381d2851049c07ff.tar.bz2
timeline-a8a8385cd959e4d9d57b8a35381d2851049c07ff.zip
...
Diffstat (limited to 'FrontEnd/src/utilities/promise.ts')
-rw-r--r--FrontEnd/src/utilities/promise.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/FrontEnd/src/utilities/promise.ts b/FrontEnd/src/utilities/promise.ts
new file mode 100644
index 00000000..96bcceac
--- /dev/null
+++ b/FrontEnd/src/utilities/promise.ts
@@ -0,0 +1,35 @@
+export function subscribePromise<T>(
+ promise: Promise<T>,
+ resolve: Parameters<Promise<T>["then"]>[0],
+ reject?: Parameters<Promise<T>["then"]>[1],
+): {
+ promise: ReturnType<Promise<T>["then"]>;
+ (): void;
+} {
+ let subscribe = true;
+
+ const p = promise.then(
+ resolve != null
+ ? (value) => {
+ if (subscribe) {
+ resolve(value);
+ }
+ }
+ : undefined,
+ reject != null
+ ? (error) => {
+ if (subscribe) {
+ reject(error);
+ }
+ }
+ : undefined,
+ );
+
+ const result = function () {
+ subscribe = false;
+ };
+ result.promise = p;
+
+ return result;
+}
+