https://github.com/olsonpm/promise-concurrency-pool
https://github.com/olsonpm/promise-concurrency-pool
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/olsonpm/promise-concurrency-pool
- Owner: olsonpm
- Created: 2016-11-09T22:26:51.000Z (over 9 years ago)
- Default Branch: dev
- Last Pushed: 2016-11-10T14:49:59.000Z (over 9 years ago)
- Last Synced: 2025-05-07T21:02:26.604Z (about 1 year ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Promise Concurrency Pool
## Why create this module?
I needed the ability to add to a pool from multiple places in my application.
I didn't feel like hacking my application to fit the api provided
by es6-promise-pool
## Contrived Example
```js
// some helpers
const log = console.log
, bPromise = require('bluebird');
const wait = ms => ({
thenLog: msg => ({
fn: () => bPromise.delay(ms).then(() => log(msg))
})
});
// now create the pool
const myPool = require('promise-concurrency-pool')
.create({ size: 2, onSettled: () => log('empty!') });
myPool.add(wait(15).thenLog('third').fn)
.add(wait(5).thenLog('first').fn)
.add(wait(2).thenLog('second').fn);
// prints
//
// first
// second
// third
// empty!
```
## API
`require('promise-concurrency-pool')` returns an object with a single
property `create`.
`create(argsObj) -> pool`
where `argsObj` is an object with the properties
```
size
*required
- the size of the pool
onSettled
- a function to call every time the pool settles (no active promises in
the pool)
```
and `pool` is an object with the properties
```
add(promiseFn) -> pool
*chainable
- 'promiseFn' is a 'promise-returning-function'
- adds promiseFn to a queue. When a promise in the pool finishes, it grabs one
from this queue until empty.
size
*readonly
- the size of the pool
```
## Test
`npm test`