Skip to content

Commit ef37eb2

Browse files
authored
Fix detached limit.map calls (#108)
1 parent 42599eb commit ef37eb2

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export default function pLimit(concurrency) {
105105
},
106106
map: {
107107
async value(iterable, function_) {
108-
const promises = Array.from(iterable, (value, index) => this(function_, value, index));
108+
const promises = Array.from(iterable, (value, index) => generator(function_, value, index));
109109
return Promise.all(promises);
110110
},
111111
},

test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,26 @@ test('map', async t => {
221221
t.deepEqual(results, [2, 3, 4, 5, 6, 7, 8]);
222222
});
223223

224+
test('map works when detached from the limit', async t => {
225+
const limit = pLimit(1);
226+
const {map} = limit;
227+
let running = 0;
228+
let maxRunning = 0;
229+
const mapper = async input => {
230+
running++;
231+
maxRunning = Math.max(maxRunning, running);
232+
await delay(10);
233+
running--;
234+
return input * 2;
235+
};
236+
237+
const directResult = limit(mapper, 1);
238+
const mapResult = map([2, 3], mapper);
239+
240+
t.deepEqual(await Promise.all([directResult, mapResult]), [2, [4, 6]]);
241+
t.is(maxRunning, 1);
242+
});
243+
224244
test('map passes index and preserves order with concurrency', async t => {
225245
const limit = pLimit(3);
226246
const inputs = [10, 10, 10, 10, 10];

0 commit comments

Comments
 (0)