pax_global_header00006660000000000000000000000064130145155410014511gustar00rootroot0000000000000052 comment=276b0aeef73ac9fe0b80622d8261a2bbe4f9f74c p-limit-1.1.0/000077500000000000000000000000001301451554100130635ustar00rootroot00000000000000p-limit-1.1.0/.editorconfig000066400000000000000000000002761301451554100155450ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 p-limit-1.1.0/.gitattributes000066400000000000000000000000351301451554100157540ustar00rootroot00000000000000* text=auto *.js text eol=lf p-limit-1.1.0/.gitignore000066400000000000000000000000151301451554100150470ustar00rootroot00000000000000node_modules p-limit-1.1.0/.travis.yml000066400000000000000000000000531301451554100151720ustar00rootroot00000000000000language: node_js node_js: - '6' - '4' p-limit-1.1.0/index.js000066400000000000000000000011251301451554100145270ustar00rootroot00000000000000'use strict'; module.exports = concurrency => { if (concurrency < 1) { throw new TypeError('Expected `concurrency` to be a number from 1 and up'); } const queue = []; let activeCount = 0; const next = () => { activeCount--; if (queue.length > 0) { queue.shift()(); } }; return fn => new Promise((resolve, reject) => { const run = () => { activeCount++; fn().then( val => { resolve(val); next(); }, err => { reject(err); next(); } ); }; if (activeCount < concurrency) { run(); } else { queue.push(run); } }); }; p-limit-1.1.0/license000066400000000000000000000021371301451554100144330ustar00rootroot00000000000000The MIT License (MIT) 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-limit-1.1.0/package.json000066400000000000000000000015411301451554100153520ustar00rootroot00000000000000{ "name": "p-limit", "version": "1.1.0", "description": "Run multiple promise-returning & async functions with limited concurrency", "license": "MIT", "repository": "sindresorhus/p-limit", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "promise", "limit", "limited", "concurrency", "throttle", "throat", "rate", "batch", "ratelimit", "task", "queue", "async", "await", "promises", "bluebird" ], "devDependencies": { "ava": "*", "delay": "^1.3.1", "in-range": "^1.0.0", "random-int": "^1.0.0", "time-span": "^1.0.0", "xo": "*" }, "xo": { "esnext": true } } p-limit-1.1.0/readme.md000066400000000000000000000025711301451554100146470ustar00rootroot00000000000000# p-limit [![Build Status](https://travis-ci.org/sindresorhus/p-limit.svg?branch=master)](https://travis-ci.org/sindresorhus/p-limit) > Run multiple promise-returning & async functions with limited concurrency ## Install ``` $ npm install --save p-limit ``` ## Usage ```js const pLimit = require('p-limit'); const limit = pLimit(1); const input = [ limit(() => fetchSomething('foo')), limit(() => fetchSomething('bar')), limit(() => doSomething()) ]; // only one promise is run at once Promise.all(input).then(result => { console.log(result); }); ``` ## API ### pLimit(concurrency) Returns a `limit` function. #### concurrency Type: `number`
Minimum: `1` Concurrency limit. ### limit(fn) Returns the promise returned by calling `fn`. #### fn Type: `Function` Promise-returning/async function. ## Related - [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control - [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions - [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions - [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency - [More…](https://github.com/sindresorhus/promise-fun) ## License MIT © [Sindre Sorhus](https://sindresorhus.com) p-limit-1.1.0/test.js000066400000000000000000000014421301451554100144010ustar00rootroot00000000000000import 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 './'; test('concurrency: 1', async t => { const input = [ [10, 300], [20, 200], [30, 100] ]; const end = timeSpan(); const limit = m(1); const mapper = ([val, ms]) => limit(() => delay(ms).then(() => val)); t.deepEqual(await Promise.all(input.map(mapper)), [10, 20, 30]); t.true(inRange(end(), 590, 650)); }); test('concurrency: 4', async t => { const concurrency = 5; let running = 0; const limit = m(concurrency); const input = Array(100).fill(0).map(() => limit(async () => { running++; t.true(running <= concurrency); await delay(randomInt(30, 200)); running--; })); await Promise.all(input); });