Skip to content

Commit

Permalink
Fix pMapIterable not accepting async values in an iterator (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
noamokman authored Dec 27, 2023
1 parent 8e08686 commit 1076833
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export function pMapIterable(
trySpawn();

try {
const returnValue = await mapper(value);
const returnValue = await mapper(await value);

runningMappersCount--;

Expand Down
30 changes: 30 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ const sites = [
'https://github.com',
];

const sitesWithPromises = [
'https://sindresorhus.com',
Promise.resolve('https://avajs.dev'),
Promise.resolve('https://github.com'),
];

const sitesAsyncIterable = {
async * [Symbol.asyncIterator]() {
yield 'https://sindresorhus.com';
yield 'https://avajs.dev';
yield 'https://github.com';
},
};

const sitesAsyncIterableWithPromises: AsyncIterable<Promise<string>> = {
[Symbol.asyncIterator]() {
return {
async next() {
return {
done: false,
value: Promise.resolve('https://github.com'),
};
},
};
},
};

const numbers = [
0,
1,
Expand Down Expand Up @@ -50,3 +77,6 @@ expectType<Promise<number[]>>(pMap(numbers, (number: number) => {
}));

expectType<AsyncIterable<string>>(pMapIterable(sites, asyncMapper));
expectType<AsyncIterable<string>>(pMapIterable(sitesWithPromises, asyncMapper));
expectType<AsyncIterable<string>>(pMapIterable(sitesAsyncIterable, asyncMapper));
expectType<AsyncIterable<string>>(pMapIterable(sitesAsyncIterableWithPromises, asyncMapper));
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import pMap, {pMapIterable, pMapSkip} from './index.js';
const sharedInput = [
[async () => 10, 300],
[20, 200],
[30, 100],
Promise.resolve([30, 100]),
];

const longerSharedInput = [
Expand Down

0 comments on commit 1076833

Please sign in to comment.