"Superposition" between sync and async
Create sync/async APIs with usable logic.
Quantum + Sync - "Superposition" between sync and async.
Heavily inspired by genasync by @loganfsmyth.
Please refer to Anthony's blog post: Async, Sync, in Between.
pnpm i quansync…getIsAsyncReturns a boolean indicating whether the current execution is in async mode.
import { getIsAsync, quansync } from 'quansync'
const fn = quansync(function* () {
const isAsync: boolean = yield* getIsAsync()
console.log(isAsync)
})
fn.sync() // false
await fn() // true
await fn.async() // trueallimport { all, quansync } from 'quansync'
const loadFiles = quansync(function* () {
return yield* all([
readFile('./one.js'),
readFile('./two.js'),
readFile('./three.js'),
])
})
const results = loadFiles.sync()
const asyncResults = await loadFiles.async()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:
…For more details on usage, refer to unplugin-quansync's docs.
Run the following command to benchmark the performance of quansync:
pnpm run build && pnpm run benchmarkBenchmark results indicate that each yield incurs an overhead of
approximately 150 ns, comparable to that of await sync(). (On Apple M1 Max)
MIT License © Anthony Fu and Kevin Deng
No open issues yet, or sync has not completed.