import fs from 'node:fs'import { quansync } from 'quansync'// Create a quansync function by providing `sync` and `async` implementationsconst readFile = quansync({ sync: (path: string) => fs.readFileSync(path), async: (path: string) => fs.promises.readFile(path),})// Create a quansync function by providing a generator functionconst myFunction = quansync(function* (filename) { // Use `yield*` to call another quansync function const code = yield* readFile(filename, 'utf8') return `// some custom prefix
getIsAsync
Returns a boolean indicating whether the current execution is in async mode.
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` implementationsconst readFile = quansync({ sync: (path: string) => fs.readFileSync(path), async: (path: string) => fs.promises.readFile(path),})// Create a quansync function by providing an **async** functionconst myFunction = quansync(async (filename) => { // Use `await` to call another quansync function const code = await readFile(filename, 'utf8')