@lsdsoftware/utils
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

Useful JavaScript utilities

Line Reader

Split text into lines

import { makeLineReader } from "@lsdsoftware/utils"

myStream.pipe(makeLineReader(line => console.log(line)))

Semaphore

Control concurrent access to resources

import { makeSemaphore } from "@lsdsoftware/utils"

const semaphore = makeSemaphore(3)

const result = await semaphore.runTask(async () => {
  //use the limited resource
})

Abortable

If you have an async operation, e.g.:

async function myTask() {
  await step1()
  await step2()
  await step3()
}

And you need to make it abortable:

import { makeAbortable } from "@lsdsoftware/utils"

const [abort, abortPromise, checkpoint] = makeAbortable()

//modify task to support early termination
async function myTask() {
  await step1
  checkpoint()  //will throw if aborted
  await step2
  checkpoint()
  await step3
}

//call abort() when you need to
setTimeout(() => abort(new Error("Timeout")), 5000)

//use Promise.race to run your task
const result = await Promise.race([
  abortPromise,
  myTask()
])

Readme

Keywords

none

Package Sidebar

Install

npm i @lsdsoftware/utils

Weekly Downloads

0

Version

1.0.5

License

MIT

Unpacked Size

18.7 kB

Total Files

21

Last publish

Collaborators

  • ken107