Skip to content

"Superposition" between sync and async in TypeScript

License

Notifications You must be signed in to change notification settings

novusnota-forks/quansync

 
 

Repository files navigation

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 an quansync function by providing `sync` and `async` implementations
const readFile = quansync({
  sync: fs.readFileSync,
  async: fs.promises.readFile,
})

// Create an 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')

Sponsors

License

MIT License © Anthony Fu

About

"Superposition" between sync and async in TypeScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 97.8%
  • JavaScript 2.2%