« get me outta code hell

data steps - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-01-29 00:00:32 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-01-29 00:00:38 -0400
commiteb3ba147e38ed658133b2e6d1470dc0e26f0e768 (patch)
tree57c38282a05ba387b7062f6e43bbb6e4d2d8f04c /src/util
parentc3dfb1d5607627e45595347628f39eb7546009da (diff)
data steps
Diffstat (limited to 'src/util')
-rw-r--r--src/util/sugar.js33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index 075aa190..8e40adeb 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -158,6 +158,10 @@ export function openAggregate({
         return aggregate.call(() => withAggregate(...args));
     };
 
+    aggregate.nestAsync = (...args) => {
+        return aggregate.callAsync(() => withAggregateAsync(...args));
+    };
+
     aggregate.map = (...args) => {
         const parent = aggregate;
         const { result, aggregate: child } = mapAggregate(...args);
@@ -165,6 +169,13 @@ export function openAggregate({
         return result;
     };
 
+    aggregate.mapAsync = async (...args) => {
+        const parent = aggregate;
+        const { result, aggregate: child } = await mapAggregateAsync(...args);
+        parent.call(child.close);
+        return result;
+    };
+
     aggregate.filter = (...args) => {
         const parent = aggregate;
         const { result, aggregate: child } = filterAggregate(...args);
@@ -317,15 +328,31 @@ function _filterAggregate(mode, promiseAll, array, fn, aggregateOpts) {
 // function with it, then closing the function and returning the result (if
 // there's no throw).
 export function withAggregate(aggregateOpts, fn) {
+    return _withAggregate('sync', aggregateOpts, fn);
+}
+
+export function withAggregateAsync(aggregateOpts, fn) {
+    return _withAggregate('async', aggregateOpts, fn);
+}
+
+export function _withAggregate(mode, aggregateOpts, fn) {
     if (typeof aggregateOpts === 'function') {
         fn = aggregateOpts;
         aggregateOpts = {};
     }
 
     const aggregate = openAggregate(aggregateOpts);
-    const result = fn(aggregate);
-    aggregate.close();
-    return result;
+
+    if (mode === 'sync') {
+        const result = fn(aggregate);
+        aggregate.close();
+        return result;
+    } else {
+        return fn(aggregate).then(result => {
+            aggregate.close();
+            return result;
+        });
+    }
 }
 
 export function showAggregate(topError, {pathToFile = null} = {}) {