promise-queue

WebJar for promise-queue

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

promise-queue
Last Version

Last Version

2.2.5
Release Date

Release Date

Type

Type

jar
Description

Description

promise-queue
WebJar for promise-queue
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/promise-queue/promise-queue

Download promise-queue

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/promise-queue/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>promise-queue</artifactId>
    <version>2.2.5</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/promise-queue/
implementation 'org.webjars.npm:promise-queue:2.2.5'
// https://jarcasting.com/artifacts/org.webjars.npm/promise-queue/
implementation ("org.webjars.npm:promise-queue:2.2.5")
'org.webjars.npm:promise-queue:jar:2.2.5'
<dependency org="org.webjars.npm" name="promise-queue" rev="2.2.5">
  <artifact name="promise-queue" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='promise-queue', version='2.2.5')
)
libraryDependencies += "org.webjars.npm" % "promise-queue" % "2.2.5"
[org.webjars.npm/promise-queue "2.2.5"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

promise-queue NPM Version Build Status Coverage Status

Promise-based queue

Installation

promise-queue can be installed using npm:

npm install promise-queue

Interface

  • new Queue(Number maxConcurrent, Number maxQueued): Queue
  • Queue#add(Function generator): Promise - adds function argument that generates a promise to the queue
  • Queue#getQueueLength(): Number - returns current length of buffer(added but not started promise generators) it <= maxQueued
  • Queue#getPendingLength(): Number - returns number of pending(concurrently running) promises it <= maxConcurrent

Example

Configure queue

By default Queue tries to use global Promises, but you can specify your own promises.

Queue.configure(require('vow').Promise);

Or use old-style promises approach:

Queue.configure(function (handler) {
    var dfd = $.Deferred();
    try {
        handler(dfd.resolve, dfd.reject, dfd.notify);
    } catch (e) {
        dfd.reject(e);
    }
    return dfd.promise();
});

Queue one by one example

var maxConcurrent = 1;
var maxQueue = Infinity;
var queue = new Queue(maxConcurrent, maxQueue);

app.get('/version/:user/:repo', function (req, res, next) {
    queue.add(function () {
        // Assume that this action is a way too expensive
        // Call of this function will be delayed on second request
        return downloadTarballFromGithub(req.params);
    })
    .then(parseJson('package.json'))
    .then(function (package) {
        res.send(package.version);
    })
    .catch(next);
});

Getting number of pending promises and queue(buffered promises) length

var maxConcurrent = 1;
var maxQueue = 1;
var queue = new Queue(maxConcurrent, maxQueue);

queue.add(function () {
    queue.getQueueLength() === 0;
    queue.getPendingLength() === 1;
    return somePromise();
});

queue.add(function () {
    queue.getQueueLength() === 0;
    queue.getPendingLength() === 0;
    return somePromise();
});

queue.getQueueLength() === 1;
queue.getPendingLength() === 1;

Live example

org.webjars.npm

Promise Queue

Promise-based Queue

Versions

Version
2.2.5