@leverj/lock.js
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

lock

lock asynchronous resources.

Simple Example

var Lock = require('lock').Lock
var lock = Lock()

lock('key', function (release) { //called when resource is available.

  //do an async operation, and wrap the callback with release.
  someAsyncOperation(args,..., release(function (err) {
    //'key' is now unlocked!
  })
})

Multiple Locks at Once.

var Lock = require('lock').Lock
var lock = Lock()

lock(['A', 'B', 'C'], function (release) { //called, when ALL
                                           //resources are available.

  //do an async operation, and wrap the callback with release(cb)
  someAsyncOperation(args,..., release(function (err) {
    //A, B & C are now unlocked!
  })
})

Lock with optional callback.

var Lock = require('lock').Lock
var lock = Lock()

lock('cache', function(release) { //called when no one is writing to cache
	//we do not care if this fails since its a cache so no callback needed
	fs.writeFile(fileCache, ... , release())
})

used in level-update

promisified Lock

const {AsyncLock}   = require('lock')
const lock = AsyncLock()
lock(['A', 'B', 'C'], function(){
    //do an async operation
    return someAsyncOperation(args)
    
}).then(function(result){
  //A, B & C are now unlocked!
}).catch(function(e){ 
// error thrown by someAsyncOperation(args) 
})

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @leverj/lock.js

Weekly Downloads

95

Version

0.0.3

License

MIT

Unpacked Size

12.2 kB

Total Files

13

Last publish

Collaborators

  • sachet.singh
  • adikeshavs
  • tshanky
  • bharathrao
  • nirmalgupta
  • darshanrathi
  • bsadeh