Skip to content

Commit 2aeffd4

Browse files
committed
Add index parameter to map() method
Fixes #96
1 parent 2b14c23 commit 2aeffd4

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ export type LimitFunction = {
2626
/**
2727
Process an array of inputs with limited concurrency.
2828
29+
The mapper function receives the item value and its index.
30+
2931
@param array - An array containing an argument for the given function.
30-
@param function_ - Promise-returning/async function.
32+
@param mapperFunction - Promise-returning/async function.
3133
@returns A Promise that returns an array of results.
3234
*/
3335
map: <Input, ReturnType> (
3436
array: Input[],
35-
function_: (input: Input) => PromiseLike<ReturnType> | ReturnType
37+
mapperFunction: (input: Input, index: number) => PromiseLike<ReturnType> | ReturnType
3638
) => Promise<ReturnType[]>;
3739

3840
/**

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default function pLimit(concurrency) {
8383
},
8484
map: {
8585
async value(array, function_) {
86-
const promises = array.map(value => this(function_, value));
86+
const promises = array.map((value, index) => this(function_, value, index));
8787
return Promise.all(promises);
8888
},
8989
},

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ Any arguments to pass through to `fn`.
5757

5858
Support for passing arguments on to the `fn` is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a *lot* of functions.
5959

60-
### limit.map(array, fn)
60+
### limit.map(array, mapperFunction)
6161

6262
Process an array of inputs with limited concurrency.
6363

64-
Returns a promise equivalent to `Promise.all(array.map(item => limit(fn, item)))`.
64+
The mapper function receives the item value and its index.
65+
66+
Returns a promise equivalent to `Promise.all(array.map((item, index) => limit(mapperFunction, item, index)))`.
6567

6668
This is a convenience function for processing inputs that arrive in batches. For more complex use cases, see [p-map](https://github.com/sindresorhus/p-map).
6769

test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@ test('map', async t => {
175175
t.deepEqual(results, [2, 3, 4, 5, 6, 7, 8]);
176176
});
177177

178+
test('map passes index and preserves order with concurrency', async t => {
179+
const limit = pLimit(3);
180+
const inputs = [10, 10, 10, 10, 10];
181+
182+
// eslint-disable-next-line unicorn/no-array-method-this-argument
183+
const results = await limit.map(inputs, async (value, index) => {
184+
// Simulate variable async duration per index to shuffle completion order
185+
await delay((inputs.length - index) * 5);
186+
return value + index;
187+
});
188+
189+
// Results should be in input order and include index
190+
t.deepEqual(results, [10, 11, 12, 13, 14]);
191+
});
192+
178193
test('throws on invalid concurrency argument', t => {
179194
t.throws(() => {
180195
pLimit(0);

0 commit comments

Comments
 (0)