{"name":"p-map","description":"Map over promises concurrently","dist-tags":{"latest":"4.0.0"},"versions":{"4.0.0":{"name":"p-map","version":"4.0.0","description":"Map over promises concurrently","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/p-map.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"engines":{"node":">=10"},"scripts":{"test":"xo && ava && tsd"},"dependencies":{"aggregate-error":"^3.0.0"},"devDependencies":{"ava":"^2.2.0","delay":"^4.1.0","in-range":"^2.0.0","random-int":"^2.0.0","time-span":"^3.1.0","tsd":"^0.7.4","xo":"^0.27.2"},"gitHead":"a4b4dec459544d98880bc53a580e53691aff9fa9","bugs":{"url":"https://github.com/sindresorhus/p-map/issues"},"_id":"p-map@4.0.0","_nodeVersion":"10.18.1","_npmVersion":"6.14.0","dist":{"integrity":"sha512-/bjOqmgETBYB5BoEeGVea8dmvHb2m9GLy1E9W43yeyfP6QQCZGFNa+XRceJEuDB6zqr+gKpIAmlLebMpykw/MQ==","shasum":"bb2f95a5eda2ec168ec9274e06a747c3e2904d2b","tarball":"http://localhost:4260/p-map/p-map-4.0.0.tgz","fileCount":5,"unpackedSize":8687,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJeYLuGCRA9TVsSAnZWagAALNkP/icS/zHu6yNrCwiCNS4E\nnbsyyry8rc6iti+rBi4HJkNecN+gWM5J+OC8mmBa29jOWiefCtMlI2vjIPs5\n/UCBVEkHoZhhwriQ5X2Yz8yW5jimdE1QErzYKHhzGsNMFdc9VuPCVGtqartl\nQkrlJT+D0LOPBtOugmaLBt6XWX2Ks1NGYr/WtFwqfsE35kDGQvZ1MaAj9+Fc\naRK5LUoEn/06LPmVqPGjKjhoue3QgihCZSxoBWjMgwYblZyAkbiEM9Xe+Gjg\nB+MCAk2Eh6gZbqzyTmAythHyQgiz+lnNwXTGDXCKAD2rf8R11V7bOl6eFiml\nuMjmDHJ84VTwt9doONukyzxbGhJqCiwc6+38yT85DZCTQpmtUgcniRPygxqC\n5LQTx6DrQQ7nFUfwF+YfUd+q+GFyzGZNknW/+J9lKJazEFd2UCOaNHguRw0F\nPFcENv4j3bX2RfNkral0JZ2I+U8vu2NkpXWG3DmL1o38SgpQ7JC/wdXjSRcn\n4VlIhz5LcBms967YXTg1LAW7OQob73ksIXp2hGaGwiVfaZhPxWowZbCahfbA\nELrajj3H88QN4QDFxmp1CNrkgPkE4AyuBSgpnTiSP9wQTeBL4lZ+mfThBwtn\n4v694zqg4uF4u2vn7v5AkNBS2OlBvYNd59WjScPCOLWWZATQKqiHVVDErAGe\nFdI9\r\n=NTiQ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC7DQo5ZwXGnm1bMkOmN+5uiS7ZpVvO4j+NbQuzyssCjQIgNdV4S73hWNZCO72WffAW3PYDOVEzEam4A1J4dnk0iCM="}]},"directories":{},"_hasShrinkwrap":false}},"readme":"# p-map\n\n> Map over promises concurrently\n\nUseful when you need to run promise-returning & async functions multiple times with different inputs concurrently.\n\nThis is different from `Promise.all()` in that you can control the concurrency and also decide whether or not to stop iterating when there's an error.\n\n## Install\n\n```sh\nnpm install p-map\n```\n\n## Usage\n\n```js\nimport pMap from 'p-map';\nimport got from 'got';\n\nconst sites = [\n\tgetWebsiteFromUsername('sindresorhus'), //=> Promise\n\t'https://avajs.dev',\n\t'https://github.com'\n];\n\nconst mapper = async site => {\n\tconst {requestUrl} = await got.head(site);\n\treturn requestUrl;\n};\n\nconst result = await pMap(sites, mapper, {concurrency: 2});\n\nconsole.log(result);\n//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']\n```\n\n## API\n\n### pMap(input, mapper, options?)\n\nReturns a `Promise` that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values returned from `mapper` in `input` order.\n\n### pMapIterable(input, mapper, options?)\n\nReturns an async iterable that streams each return value from `mapper` in order.\n\n```js\nimport {pMapIterable} from 'p-map';\n\n// Multiple posts are fetched concurrently, with limited concurrency and backpressure\nfor await (const post of pMapIterable(postIds, getPostMetadata, {concurrency: 8})) {\n\tconsole.log(post);\n};\n```\n\n#### input\n\nType: `AsyncIterable | unknown> | Iterable | unknown>`\n\nSynchronous or asynchronous iterable that is iterated over concurrently, calling the `mapper` function for each element. Each iterated item is `await`'d before the `mapper` is invoked so the iterable may return a `Promise` that resolves to an item.\n\nAsynchronous iterables (different from synchronous iterables that return `Promise` that resolves to an item) can be used when the next item may not be ready without waiting for an asynchronous process to complete and/or the end of the iterable may be reached after the asynchronous process completes. For example, reading from a remote queue when the queue has reached empty, or reading lines from a stream.\n\n#### mapper(element, index)\n\nType: `Function`\n\nExpected to return a `Promise` or value.\n\n#### options\n\nType: `object`\n\n##### concurrency\n\nType: `number` *(Integer)*\\\nDefault: `Infinity`\\\nMinimum: `1`\n\nNumber of concurrently pending promises returned by `mapper`.\n\n##### backpressure\n\n**Only for `pMapIterable`**\n\nType: `number` *(Integer)*\\\nDefault: `options.concurrency`\\\nMinimum: `options.concurrency`\n\nMaximum number of promises returned by `mapper` that have resolved but not yet collected by the consumer of the async iterable. Calls to `mapper` will be limited so that there is never too much backpressure.\n\nUseful whenever you are consuming the iterable slower than what the mapper function can produce concurrently. For example, to avoid making an overwhelming number of HTTP requests if you are saving each of the results to a database.\n\n##### stopOnError\n\n**Only for `pMap`**\n\nType: `boolean`\\\nDefault: `true`\n\nWhen `true`, the first mapper rejection will be rejected back to the consumer.\n\nWhen `false`, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an [`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) containing all the errors from the rejected promises.\n\nCaveat: When `true`, any already-started async mappers will continue to run until they resolve or reject. In the case of infinite concurrency with sync iterables, *all* mappers are invoked on startup and will continue after the first rejection. [Issue #51](https://github.com/sindresorhus/p-map/issues/51) can be implemented for abort control.\n\n##### signal\n\n**Only for `pMap`**\n\nType: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)\n\nYou can abort the promises using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).\n\n```js\nimport pMap from 'p-map';\nimport delay from 'delay';\n\nconst abortController = new AbortController();\n\nsetTimeout(() => {\n\tabortController.abort();\n}, 500);\n\nconst mapper = async value => value;\n\nawait pMap([delay(1000), delay(1000)], mapper, {signal: abortController.signal});\n// Throws AbortError (DOMException) after 500 ms.\n```\n\n### pMapSkip\n\nReturn this value from a `mapper` function to skip including the value in the returned array.\n\n```js\nimport pMap, {pMapSkip} from 'p-map';\nimport got from 'got';\n\nconst sites = [\n\tgetWebsiteFromUsername('sindresorhus'), //=> Promise\n\t'https://avajs.dev',\n\t'https://example.invalid',\n\t'https://github.com'\n];\n\nconst mapper = async site => {\n\ttry {\n\t\tconst {requestUrl} = await got.head(site);\n\t\treturn requestUrl;\n\t} catch {\n\t\treturn pMapSkip;\n\t}\n};\n\nconst result = await pMap(sites, mapper, {concurrency: 2});\n\nconsole.log(result);\n//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']\n```\n\n## Related\n\n- [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency\n- [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently\n- [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently\n- [p-props](https://github.com/sindresorhus/p-props) - Like `Promise.all()` but for `Map` and `Object`\n- [p-map-series](https://github.com/sindresorhus/p-map-series) - Map over promises serially\n- [Moreā€¦](https://github.com/sindresorhus/promise-fun)\n","homepage":"https://github.com/sindresorhus/p-map#readme","repository":{"type":"git","url":"git+https://github.com/sindresorhus/p-map.git"},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"bugs":{"url":"https://github.com/sindresorhus/p-map/issues"},"license":"MIT","readmeFilename":"readme.md"}