UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

35 lines 1.44 kB
//#region src/array/mapAsync.d.ts interface MapAsyncOptions { concurrency?: number; } /** * Transforms each element in an array using an async callback function and returns * a promise that resolves to an array of transformed values. * * @template T - The type of elements in the input array. * @template R - The type of elements in the output array. * @param array The array to transform. * @param callback An async function that transforms each element. * @param [options] Optional configuration object. * @param [options.concurrency] Maximum number of concurrent async operations. If not specified, all operations run concurrently. * @returns A promise that resolves to an array of transformed values. * @example * const users = [{ id: 1 }, { id: 2 }, { id: 3 }]; * const userDetails = await mapAsync(users, async (user) => { * return await fetchUserDetails(user.id); * }); * // Returns: [{ id: 1, name: '...' }, { id: 2, name: '...' }, { id: 3, name: '...' }] * * @example * // With concurrency limit * const numbers = [1, 2, 3, 4, 5]; * const results = await mapAsync( * numbers, * async (n) => await slowOperation(n), * { concurrency: 2 } * ); * // Processes at most 2 operations concurrently */ declare function mapAsync<T, R>(array: readonly T[], callback: (item: T, index: number, array: readonly T[]) => Promise<R>, options?: MapAsyncOptions): Promise<R[]>; //#endregion export { mapAsync };