This is a simple queue where you can push a function(resolve, reject)
to a queue and
get a promise that references that task.
Install
npm install simple-promise-queue
Use Cases
- Use case #1: Throttled
Promise.all
- Use case #2: Used as a task queuer to grab a resource with limited connection pools
How to use
Queue.pushTask(function(resolve, reject) { ... })
Puts a task at the end of the queue. Returns a Promise.
Queue.unshiftTask(function(resolve, reject) { ... })
Puts a task at the beginning of the queue. Returns a Promise.
simple-promise-queue
inherits from queue, so check that out for other methods.
Note: I added an option autoStart
that can be passed into the constructor so the queue
will automatically start whenever something is pushed into it.
Examples
Adding your own Promise implementation:
var Queue = ; Queue;
Pushing a task to the queue and getting a promise:
var Queue = ; var queue = autoStart: true // autostart the queue; var promise = queue; promise;
A useful example of this would be if you had Promise.all
but wanted to only throttle
it to run 5 tasks at a time:
var Queue = ; var queue = autoStart: true // autostart the queue concurrency: 5; var promiseArr = ; var { var promise = queue; promiseArr;}; var promiseArr = ;for var id = 0; id < 100; id++ queue; PromiseallpromiseArr;
Here is an example where you have 2 jobs (each having 2 tasks) to complete and want to know when each job is finished individually.
var Queue = ; var queue = autoStart: true // autostart the queue concurrency: 3; var job1Part1 = queue; var job1Part2 = queue; var job2Part1 = queue; var job2Part2 = queue; Promisealljob1Part1 job1Part2; Promisealljob2Part1 job2Part2;
Testing
- Run the command inside this folder
./node_modules/mocha/bin/mocha