From eb3ba147e38ed658133b2e6d1470dc0e26f0e768 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 29 Jan 2022 00:00:32 -0400 Subject: data steps --- src/util/sugar.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'src/util') 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} = {}) { -- cgit 1.3.0-6-gf8a5