0.2.8 • Published 9 months ago

quansync v0.2.8

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

quansync

npm version npm downloads bundle JSDocs License

Create sync/async APIs with usable logic.

Quantum + Sync - "Superposition" between sync and async.

  • Typesafe
  • ESM, modern JavaScript
  • Zero dependencies

Heavily inspired by genasync by @loganfsmyth.

Usage

pnpm i quansync
import fs from 'node:fs'
import { quansync } from 'quansync'

// Create a quansync function by providing `sync` and `async` implementations
const readFile = quansync({
  sync: (path: string) => fs.readFileSync(path),
  async: (path: string) => fs.promises.readFile(path),
})

// Create a quansync function by providing a generator function
const myFunction = quansync(function* (filename) {
  // Use `yield*` to call another quansync function
  const code = yield* readFile(filename, 'utf8')

  return `// some custom prefix\n${code}`
})

// Use it as a sync function
const result = myFunction.sync('./some-file.js')

// Use it as an async function
const asyncResult = await myFunction.async('./some-file.js')

Why

// TODO:

How it works

// TODO:

Benchmark

Run the following command to benchmark the performance of quansync:

pnpm run build && pnpm run benchmark

Benchmark results indicate that each yield incurs an overhead of approximately 150 ns, comparable to that of await sync(). (On Apple M1 Max)

Build-time Macro

If you don't like the function* and yield* syntax, we also provide a build-time macro via unplugin-quansync allowing you use quansync with async/await syntax, while still able to get the sync version out of that.

Here is an example:

import fs from 'node:fs'
import { quansync } from 'quansync/macro'

// Create a quansync function by providing `sync` and `async` implementations
const readFile = quansync({
  sync: (path: string) => fs.readFileSync(path),
  async: (path: string) => fs.promises.readFile(path),
})

// Create a quansync function by providing an **async** function
const myFunction = quansync(async (filename) => {
  // Use `await` to call another quansync function
  const code = await readFile(filename, 'utf8')

  return `// some custom prefix\n${code}`
})

// Use it as a sync function
const result = myFunction.sync('./some-file.js')

// Use it as an async function
const asyncResult = await myFunction.async('./some-file.js')

For more details on usage, refer to unplugin-quansync's docs.

Sponsors

License

MIT License © Anthony Fu and Kevin Deng

0.2.8

9 months ago

0.2.7

10 months ago

0.2.6

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.0

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago

0.0.0

10 months ago