From 10768330ebf3e210b237cb168b75ba2a13ecf666 Mon Sep 17 00:00:00 2001 From: Noam Okman Date: Wed, 27 Dec 2023 02:34:29 +0200 Subject: [PATCH] Fix `pMapIterable` not accepting async values in an iterator (#69) --- index.js | 2 +- index.test-d.ts | 30 ++++++++++++++++++++++++++++++ test.js | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5cc265b..dc0e07e 100644 --- a/index.js +++ b/index.js @@ -212,7 +212,7 @@ export function pMapIterable( trySpawn(); try { - const returnValue = await mapper(value); + const returnValue = await mapper(await value); runningMappersCount--; diff --git a/index.test-d.ts b/index.test-d.ts index 58acea4..7cf4f99 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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> = { + [Symbol.asyncIterator]() { + return { + async next() { + return { + done: false, + value: Promise.resolve('https://github.com'), + }; + }, + }; + }, +}; + const numbers = [ 0, 1, @@ -50,3 +77,6 @@ expectType>(pMap(numbers, (number: number) => { })); expectType>(pMapIterable(sites, asyncMapper)); +expectType>(pMapIterable(sitesWithPromises, asyncMapper)); +expectType>(pMapIterable(sitesAsyncIterable, asyncMapper)); +expectType>(pMapIterable(sitesAsyncIterableWithPromises, asyncMapper)); diff --git a/test.js b/test.js index 03d7d59..a33b07b 100644 --- a/test.js +++ b/test.js @@ -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 = [