0.1.2 • Published 6 years ago

itz-locking-time v0.1.2

Weekly downloads
1
License
MPL-2.0
Repository
-
Last release
6 years ago

itz-locking-time

Use promise-based non-ipc locks

API

.runOnce(id, fnc, t): Run a function once or await it's current execution

  • id unique identifier
  • fnc() function that is going to be executed once or if it's already executing is being awaited
  • t: Throw or stay silent

.runNext(id, fnc, t): Run function or if already running run it again afterwards. Will automatically batch all next calls per iteration

  • id unique identifier
  • fnc() function that is going to be executed once or if it's already executing is being awaited
  • t: Throw or stay silent

Usage

const Lock = require('itz-locking-time')
const lock = Lock()

const wait = (i) => new Promise((resolve, reject) => setTimeout(resolve, i))

const someFnc = async () => {
  await wait(10000)
}

lock.runOnce('heavy', someFnc) // will run
await lock.runOnce('heavy', someFnc) // will await first execution

lock.runNext('process', someFnc) // will run
lock.runNext('process', someFnc) // will queue next execution
await lock.runNext('process', someFnc) // will await next execution