0.2.0 • Published 5 months ago

@vulppi/promise-lock v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Vulppi Promise Lock

A simple way for wait async tasks to protect multiple access to a shared resource.

Usage

You can use the following:

import { createLocker } from '@vulppi/promise-lock'

const locker = createLocker({ timeout: 20000 }) // 20 seconds timeout

async function randomDelay(index: number) {
  const lock = await locker.lock()

  const delay = Math.floor(Math.random() * 1000)

  await new Promise((resolve) => setTimeout(resolve, delay))

  console.log(
    'Index:',
    index,
    '|',
    'Delayed:',
    delay,
    '|',
    'Pool length:',
    lock.length,
  )

  lock.unlock()
}

for (let i = 0; i < 10; i++) {
  randomDelay(i)
}

/*
 * Final output:
 *
 * Index: 0 | Delayed: 492 | Pool length: 10
 * Index: 1 | Delayed: 797 | Pool length: 9
 * Index: 2 | Delayed: 538 | Pool length: 8
 * Index: 3 | Delayed: 202 | Pool length: 7
 * Index: 4 | Delayed: 454 | Pool length: 6
 * Index: 5 | Delayed: 249 | Pool length: 5
 * Index: 6 | Delayed: 597 | Pool length: 4
 * Index: 7 | Delayed: 669 | Pool length: 3
 * Index: 8 | Delayed: 411 | Pool length: 2
 * Index: 9 | Delayed: 234 | Pool length: 1
 *
 * Note: Only one task per time can access the resource
 */
0.2.0

5 months ago

0.1.1

11 months ago

0.1.0

11 months ago

0.0.2

11 months ago

0.0.1

12 months ago