pax_global_header00006660000000000000000000000064131556720300014514gustar00rootroot0000000000000052 comment=e0a1c91c00d5509e9f04c6f0392693a452f385f0 p-map-1.2.0/000077500000000000000000000000001315567203000125265ustar00rootroot00000000000000p-map-1.2.0/.editorconfig000066400000000000000000000002571315567203000152070ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 p-map-1.2.0/.gitattributes000066400000000000000000000000351315567203000154170ustar00rootroot00000000000000* text=auto *.js text eol=lf p-map-1.2.0/.gitignore000066400000000000000000000000271315567203000145150ustar00rootroot00000000000000node_modules yarn.lock p-map-1.2.0/.npmrc000066400000000000000000000000231315567203000136410ustar00rootroot00000000000000package-lock=false p-map-1.2.0/.travis.yml000066400000000000000000000000631315567203000146360ustar00rootroot00000000000000language: node_js node_js: - '8' - '6' - '4' p-map-1.2.0/index.js000066400000000000000000000023261315567203000141760ustar00rootroot00000000000000'use strict'; module.exports = (iterable, mapper, opts) => new Promise((resolve, reject) => { opts = Object.assign({ concurrency: Infinity }, opts); if (typeof mapper !== 'function') { throw new TypeError('Mapper function is required'); } const concurrency = opts.concurrency; if (!(typeof concurrency === 'number' && concurrency >= 1)) { throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${concurrency}\` (${typeof concurrency})`); } const ret = []; const iterator = iterable[Symbol.iterator](); let isRejected = false; let iterableDone = false; let resolvingCount = 0; let currentIdx = 0; const next = () => { if (isRejected) { return; } const nextItem = iterator.next(); const i = currentIdx; currentIdx++; if (nextItem.done) { iterableDone = true; if (resolvingCount === 0) { resolve(ret); } return; } resolvingCount++; Promise.resolve(nextItem.value) .then(el => mapper(el, i)) .then( val => { ret[i] = val; resolvingCount--; next(); }, err => { isRejected = true; reject(err); } ); }; for (let i = 0; i < concurrency; i++) { next(); if (iterableDone) { break; } } }); p-map-1.2.0/license000066400000000000000000000021251315567203000140730ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. p-map-1.2.0/package.json000066400000000000000000000013441315567203000150160ustar00rootroot00000000000000{ "name": "p-map", "version": "1.2.0", "description": "Map over promises concurrently", "license": "MIT", "repository": "sindresorhus/p-map", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "promise", "map", "resolved", "wait", "collection", "iterable", "iterator", "race", "fulfilled", "async", "await", "promises", "concurrently", "concurrency", "parallel", "bluebird" ], "devDependencies": { "ava": "*", "delay": "^2.0.0", "in-range": "^1.0.0", "random-int": "^1.0.0", "time-span": "^2.0.0", "xo": "*" } } p-map-1.2.0/readme.md000066400000000000000000000042261315567203000143110ustar00rootroot00000000000000# p-map [![Build Status](https://travis-ci.org/sindresorhus/p-map.svg?branch=master)](https://travis-ci.org/sindresorhus/p-map) > Map over promises concurrently Useful when you need to run promise-returning & async functions multiple times with different inputs concurrently. ## Install ``` $ npm install p-map ``` ## Usage ```js const pMap = require('p-map'); const got = require('got'); const sites = [ getWebsiteFromUsername('sindresorhus'), //=> Promise 'ava.li', 'todomvc.com', 'github.com' ]; const mapper = el => got.head(el).then(res => res.requestUrl); pMap(sites, mapper, {concurrency: 2}).then(result => { console.log(result); //=> ['http://sindresorhus.com/', 'http://ava.li/', 'http://todomvc.com/', 'http://github.com/'] }); ``` ## API ### pMap(input, mapper, [options]) Returns 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. #### input Type: `Iterable` Iterated over concurrently in the `mapper` function. #### mapper(element, index) Type: `Function` Expected to return a `Promise` or value. #### options Type: `Object` ##### concurrency Type: `number`
Default: `Infinity`
Minimum: `1` Number of concurrently pending promises returned by `mapper`. ## Related - [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency - [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently - [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently - [p-props](https://github.com/sindresorhus/p-props) - Like `Promise.all()` but for `Map` and `Object` - [p-map-series](https://github.com/sindresorhus/p-map-series) - Map over promises serially - [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control - [More…](https://github.com/sindresorhus/promise-fun) ## License MIT © [Sindre Sorhus](https://sindresorhus.com) p-map-1.2.0/test.js000066400000000000000000000041031315567203000140410ustar00rootroot00000000000000import test from 'ava'; import delay from 'delay'; import inRange from 'in-range'; import timeSpan from 'time-span'; import randomInt from 'random-int'; import m from './'; const input = [ Promise.resolve([10, 300]), [20, 200], [30, 100] ]; const mapper = ([val, ms]) => delay(ms).then(() => val); test('main', async t => { const end = timeSpan(); t.deepEqual(await m(input, mapper), [10, 20, 30]); t.true(inRange(end(), 290, 430)); }); test('concurrency: 1', async t => { const end = timeSpan(); t.deepEqual(await m(input, mapper, {concurrency: 1}), [10, 20, 30]); t.true(inRange(end(), 590, 760)); }); test('concurrency: 4', async t => { const concurrency = 4; let running = 0; await m(Array(100).fill(0), async () => { running++; t.true(running <= concurrency); await delay(randomInt(30, 200)); running--; }, {concurrency}); }); test('handles empty iterable', async t => { t.deepEqual(await m([], mapper), []); }); test('async with concurrency: 2 (random time sequence)', async t => { const input = Array(10).map(() => randomInt(0, 100)); const mapper = value => delay(value).then(() => value); const result = await m(input, mapper, {concurrency: 2}); t.deepEqual(result, input); }); test('async with concurrency: 2 (problematic time sequence)', async t => { const input = [100, 200, 10, 36, 13, 45]; const mapper = value => delay(value).then(() => value); const result = await m(input, mapper, {concurrency: 2}); t.deepEqual(result, input); }); test('async with concurrency: 2 (out of order time sequence)', async t => { const input = [200, 100, 50]; const mapper = value => delay(value).then(() => value); const result = await m(input, mapper, {concurrency: 2}); t.deepEqual(result, input); }); test('enforce number in options.concurrency', async t => { await t.throws(m([], () => {}, {concurrency: 0}), TypeError); await t.throws(m([], () => {}, {concurrency: undefined}), TypeError); await t.notThrows(m([], () => {}, {concurrency: 1})); await t.notThrows(m([], () => {}, {concurrency: 10})); await t.notThrows(m([], () => {}, {concurrency: Infinity})); });