« get me outta code hell

sugar: promiseWithResolvers - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-02 15:15:12 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:48 -0300
commit284f32fc0aa6d6aa513961d53dfc091cd09580c2 (patch)
treeb18554fc994b62ec1eff6322f5a82087f36e2aaf
parent4c068743b70f85159821ac0b739cc50840a59ee7 (diff)
sugar: promiseWithResolvers
-rw-r--r--src/util/sugar.js10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index 183a75d5..1c057ed1 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -266,6 +266,16 @@ export function delay(ms) {
   return new Promise((res) => setTimeout(res, ms));
 }
 
+export function promiseWithResolvers() {
+  let obj = {};
+
+  obj.promise =
+    new Promise((...opts) =>
+      ([obj.resolve, obj.reject] = opts));
+
+  return obj;
+}
+
 // Stolen from here: https://stackoverflow.com/a/3561711
 //
 // There's a proposal for a native JS function like this, 8ut it's not even