Resource management with co.
Last updated 2 years ago by jlarsson .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install co-using 
SYNC missed versions from official npm registry.

Resource management with co.

npm travis license js-standard-style


  • automatic allocation and destruction of resources
  • co/koa friendly
  • easy schema for resource management
  • builtin synchronization resources - mutex, semaphore and read/write lock
// actual implementation from using.js
function using (resource, gen) {
  return co(function * () {
    let handle = yield resource.acquire()
    try {
      return yield gen(handle)
    } finally {


let using = require('co-using')

// Define how connections are created and destroyed
let connectionPool = {
  acquire: function () {
    return new Connection().openAsync()
  release: function (connection) {

// Use a connection, ensuring it gets destroyed
using(connectionPool, function * (connection) {
  yield connection.queryAsync(...)


require('co-using')(<resource manager>, <co 4 compatible generator>)

A resource manager is any object implementing acquire and release. The result of acquire should be a thenable-

let resourceManager = {
  acquire: function () { return Promise.resolve(someValue) },
  release: function (resource) { ... }


Mutexes (mutual exclusion), semaphores (limited parallel access) and read write locks are synchronization primitives that follows the acquire/release pattern very well. For your convenience, they are all included.

All primitives can be named and optionally scoped to a naming container. This is useful when you want to control access to resources not known in advance, such as files or database records. Memory is reclaimed for named resources not active in any execution path.

Use DEBUG=co-using:* node --harmony <...> to get debug output.


Mutually exclusive access to generator code.

let using = require('co-using')
let mutex = require('co-using/mutex')
let m = mutex()
using(m, function * () { ... })

A mutex can also be (globally) named,

let m = mutex('mutex number one')

A mutex can also be named in a specific scope,

let mutexNamingScope = {}
let m = mutex('mutex number one', mutexNamingScope)


Limited parallel access to generator code.

let using = require('co-using')
let semaphore = require('co-using/semaphore')
let sem = semaphore(10)
using(sem, function * () { ... })

A semaphore can also be (globally) named,

let sem = semaphore(10, 'semaphore number one')

A semaphore can also be named in a specific scope,

let semaphoreNamingScope = {}
let sem = semaphore('semaphore number one', semaphoreNamingScope)

Read/Write lock

Parallel access to generator code, with the following restrictions

  • if any writer is active, no other readers or writers can be active
  • if any reader is active, only other readers up to specified limit can be active
  • scheduling is fair, i.e. access is granted in the requested order
let using = require('co-using')
let rwlock = require('co-using/rwlock')
let l = rwlock()
using(, function * () { ... })
using(l.write(), function * () { ... })

It is possible to specify max concurrency (default is many) of readers (note that for writers this is always 1).

let l = rwlock(10)

A read/write lock can also be (globally) named,

let l = rwlock(10, 'lock number one')

A read/write lock can also be named in a specific scope,

let lockNamingScope = {}
let lock = rwlock(10, 'lock number one', lockNamingScope)
Maintainers (1)
Today 4
This Week 4
This Month 8
Last Day 0
Last Week 0
Last Month 8
Dependencies (3)
Dev Dependencies (1)
Dependents (0)

Copyright 2014 - 2017 © |