« get me outta code hell

sugar: make withEntries work async - 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 14:16:31 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:47 -0300
commita83642ce3f54050054c042dd83ce5cfce6eafab8 (patch)
tree988c0fa6cb2c419ecd4063ee3b898ccc544db3c5
parent9fa29f389395de966b92a035a211a7ab403b48b8 (diff)
sugar: make withEntries work async
-rw-r--r--src/util/sugar.js10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index d0b7ffab..183a75d5 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -183,8 +183,14 @@ export const compareArrays = (arr1, arr2, {checkOrder = true} = {}) =>
     : arr1.every((x) => arr2.includes(x)));
 
 // Stolen from jq! Which pro8a8ly stole the concept from other places. Nice.
-export const withEntries = (obj, fn) =>
-  Object.fromEntries(fn(Object.entries(obj)));
+export const withEntries = (obj, fn) => {
+  const result = fn(Object.entries(obj));
+  if (result instanceof Promise) {
+    return result.then(entries => Object.fromEntries(entries));
+  } else {
+    return Object.fromEntries(result);
+  }
+}
 
 export function setIntersection(set1, set2) {
   const intersection = new Set();