2022-12-08 17:17:11 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function pipeAsync(...methods) {
|
|
|
|
return async (data) => {
|
|
|
|
let res = data;
|
|
|
|
|
|
|
|
for (const method of methods) {
|
|
|
|
res = await method(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-19 17:24:12 +01:00
|
|
|
* @type { import('./async').mapAsync }
|
2022-12-08 17:17:11 +01:00
|
|
|
*/
|
|
|
|
function mapAsync(promiseArray) {
|
|
|
|
return (callback) => {
|
|
|
|
const transformedPromiseArray = promiseArray.map(async (promiseValue, index) => {
|
|
|
|
const value = await promiseValue;
|
|
|
|
return callback(value, index);
|
|
|
|
});
|
|
|
|
return Promise.all(transformedPromiseArray);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-19 17:24:12 +01:00
|
|
|
* @type { import('./async').reduceAsync }
|
2022-12-08 17:17:11 +01:00
|
|
|
*/
|
|
|
|
function reduceAsync(promiseArray) {
|
|
|
|
return (callback, initialValue) =>
|
|
|
|
promiseArray.reduce(async (previousPromise, currentValue, index) => {
|
|
|
|
const previousValue = await previousPromise;
|
|
|
|
return callback(previousValue, await currentValue, index);
|
|
|
|
}, Promise.resolve(initialValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
mapAsync,
|
|
|
|
reduceAsync,
|
|
|
|
pipeAsync,
|
|
|
|
};
|