Skip to content

Commit 1076833

Browse files
authored
Fix pMapIterable not accepting async values in an iterator (#69)
1 parent 8e08686 commit 1076833

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export function pMapIterable(
212212
trySpawn();
213213

214214
try {
215-
const returnValue = await mapper(value);
215+
const returnValue = await mapper(await value);
216216

217217
runningMappersCount--;
218218

index.test-d.ts

+30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ const sites = [
77
'https://github.com',
88
];
99

10+
const sitesWithPromises = [
11+
'https://sindresorhus.com',
12+
Promise.resolve('https://avajs.dev'),
13+
Promise.resolve('https://github.com'),
14+
];
15+
16+
const sitesAsyncIterable = {
17+
async * [Symbol.asyncIterator]() {
18+
yield 'https://sindresorhus.com';
19+
yield 'https://avajs.dev';
20+
yield 'https://github.com';
21+
},
22+
};
23+
24+
const sitesAsyncIterableWithPromises: AsyncIterable<Promise<string>> = {
25+
[Symbol.asyncIterator]() {
26+
return {
27+
async next() {
28+
return {
29+
done: false,
30+
value: Promise.resolve('https://github.com'),
31+
};
32+
},
33+
};
34+
},
35+
};
36+
1037
const numbers = [
1138
0,
1239
1,
@@ -50,3 +77,6 @@ expectType<Promise<number[]>>(pMap(numbers, (number: number) => {
5077
}));
5178

5279
expectType<AsyncIterable<string>>(pMapIterable(sites, asyncMapper));
80+
expectType<AsyncIterable<string>>(pMapIterable(sitesWithPromises, asyncMapper));
81+
expectType<AsyncIterable<string>>(pMapIterable(sitesAsyncIterable, asyncMapper));
82+
expectType<AsyncIterable<string>>(pMapIterable(sitesAsyncIterableWithPromises, asyncMapper));

test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import pMap, {pMapIterable, pMapSkip} from './index.js';
88
const sharedInput = [
99
[async () => 10, 300],
1010
[20, 200],
11-
[30, 100],
11+
Promise.resolve([30, 100]),
1212
];
1313

1414
const longerSharedInput = [

0 commit comments

Comments
 (0)