chore(async-utils): move jsdoc to ts typings

This commit is contained in:
nathan-pichon 2022-12-19 17:24:12 +01:00
parent 50ee8882cd
commit 1f4b7c196e
No known key found for this signature in database
2 changed files with 12 additions and 28 deletions

10
packages/core/utils/lib/async.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
type PromiseArray<T> = (T | Promise<T>)[];
type MapIteratee<T, R = T> = (value: T, index: number) => R | Promise<R>;
type ReduceIteratee<P, C = P, R = P> = (previousResult: P, currentValue: C, index: number) => R | Promise<R>;
export declare function mapAsync<T = unknown>(promiseArray: PromiseArray<T>): <R = T>(iteratee: MapIteratee<T, R>) => Promise<R[]>;
export declare function reduceAsync<T = unknown>(promiseArray: PromiseArray<T>): <R = unknown, I>(iteratee: ReduceIteratee<I | R, T, R>, initialValue?: I) => Promise<R>;

View File

@ -13,21 +13,9 @@ function pipeAsync(...methods) {
}
/**
* Map function callback.
* @callback mapAsyncCallback
* @param {*} value
* @param {number} index
*/
/**
* Async iteration over an array of promises
* @param {promise<*>[]} promiseArray
* @returns {function(callback: mapAsyncCallback): promise<*[]>}
* @type { import('./async').mapAsync }
*/
function mapAsync(promiseArray) {
/**
* @param {mapAsyncCallback} callback
* @returns promise<*[]>
*/
return (callback) => {
const transformedPromiseArray = promiseArray.map(async (promiseValue, index) => {
const value = await promiseValue;
@ -38,23 +26,9 @@ function mapAsync(promiseArray) {
}
/**
* Reduce function callback.
* @callback reduceAsyncCallback
* @param {*} previousValue
* @param {*} currentValue
* @param {number} index
*/
/**
* Async chain over an array of promises
* @param {promise<*>[]} promiseArray
* @returns {function(callback: reduceAsyncCallback, initialValue?: *): promise<*>}
* @type { import('./async').reduceAsync }
*/
function reduceAsync(promiseArray) {
/**
* @param {reduceAsyncCallback} callback
* @param {*} [initialValue]
* @returns promise<*>
*/
return (callback, initialValue) =>
promiseArray.reduce(async (previousPromise, currentValue, index) => {
const previousValue = await previousPromise;