47 lines
864 B
JavaScript
Raw Normal View History

'use strict';
const pMap = require('p-map');
const { curry } = require('lodash/fp');
function pipeAsync(...methods) {
return async (data) => {
let res = data;
for (const method of methods) {
res = await method(res);
}
return res;
};
}
/**
* @type { import('./async').MapAsync }
*/
const mapAsync = curry(pMap);
2023-01-31 15:14:26 +01:00
/**
* @type { import('./async').ReduceAsync }
*/
const reduceAsync = curry(async (mixedArray, iteratee, initialValue) => {
let acc = initialValue;
for (let i = 0; i < mixedArray.length; i += 1) {
acc = await iteratee(acc, await mixedArray[i], i);
2023-01-31 15:14:26 +01:00
}
return acc;
}, 2);
2023-01-31 15:14:26 +01:00
2023-02-06 15:59:47 +01:00
/**
* @type { import('./async').ForEachAsync }
*/
2023-02-06 19:11:58 +01:00
const forEachAsync = curry(async (array, func, options) => {
await mapAsync(array, func, options);
2023-02-06 15:59:47 +01:00
});
module.exports = {
mapAsync,
2023-01-31 15:14:26 +01:00
reduceAsync,
2023-02-06 15:59:47 +01:00
forEachAsync,
pipeAsync,
};