Socket
Socket
Sign inDemoInstall

conc

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

conc

Wonderful reusable concurrency utilities


Version published
Weekly downloads
2
decreased by-77.78%
Maintainers
1
Weekly downloads
 
Created
Source

Concurrency utilities for Javascript.

asyncqueue

asyncqueue is an asynchronous FIFO queue brokered by Promises. Feed the queue with offer(item), and retrieve it with poll(). Because poll may be called on an empty queue, the returned Promise provides a way to suspend the poll until a rendezvous with the next call to offer.

To illustrate its power and simplicity, below is an implementation of a resource pool. We use the pool by passing it a closure, to which the resource is applied, afterwards the resource goes back into the pool, e.g.:

// Initialize the pool with a capacity and an action that creates
// a new resource.
var withResource = createPool(10, () => new DbConnection);
// Pass `withResource` into some other part of the application
withResource(conn => conn.sql('SELECT ...')).then(result => {
  // ...Read the result...
});

The implementation of createPool.

var asyncqueue = require('conc').asyncqueue;
function createPool(cap, newResource) {
  var queue = asyncqueue();
  for (var i = 0; i < cap; i++) queue.offer(newResource());
  return function(perform) {
    // Retrieve a resource from the pool.
    return queue.poll().then(resource =>
      // Perform an action with the resource.
      perform(resource).finally(() =>
        // Finally, return the resource to the pool.
        queue.offer(resource)));
  };
}

Keywords

FAQs

Package last updated on 17 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc