2022-12-08 17:17:11 +01:00
|
|
|
'use strict';
|
|
|
|
|
2023-01-17 15:07:27 +01:00
|
|
|
const pMap = require('p-map');
|
|
|
|
const { curry } = require('lodash/fp');
|
2022-12-20 12:00:49 +01:00
|
|
|
|
2022-12-08 17:17:11 +01:00
|
|
|
function pipeAsync(...methods) {
|
|
|
|
return async (data) => {
|
|
|
|
let res = data;
|
|
|
|
|
|
|
|
for (const method of methods) {
|
|
|
|
res = await method(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-17 15:07:27 +01:00
|
|
|
* @type { import('./async').MapAsync }
|
2022-12-08 17:17:11 +01:00
|
|
|
*/
|
2023-01-17 15:07:27 +01:00
|
|
|
const mapAsync = curry(pMap);
|
2022-12-08 17:17:11 +01:00
|
|
|
|
|
|
|
/**
|
2022-12-19 17:24:12 +01:00
|
|
|
* @type { import('./async').reduceAsync }
|
2022-12-08 17:17:11 +01:00
|
|
|
*/
|
|
|
|
function reduceAsync(promiseArray) {
|
2022-12-20 12:03:59 +01:00
|
|
|
return (iteratee, initialValue) =>
|
2022-12-08 17:17:11 +01:00
|
|
|
promiseArray.reduce(async (previousPromise, currentValue, index) => {
|
|
|
|
const previousValue = await previousPromise;
|
2022-12-20 12:03:59 +01:00
|
|
|
return iteratee(previousValue, await currentValue, index);
|
2022-12-08 17:17:11 +01:00
|
|
|
}, Promise.resolve(initialValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
mapAsync,
|
|
|
|
reduceAsync,
|
|
|
|
pipeAsync,
|
|
|
|
};
|