Skip to content

Commit bf4e04e

Browse files
committed
docs: update
1 parent a6cb465 commit bf4e04e

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,56 @@ const asyncResult = await myFunction.async('./some-file.js')
135135

136136
For more details on usage, refer to [quansync's docs](https://github.com/antfu-collective/quansync#usage).
137137

138+
## How it works
139+
140+
`unplugin-quansync` transforms your async functions into generator functions with `quansyncMacro`,
141+
and transforms `await` into `yield * toGenerator(...)`.
142+
143+
The example above is transformed into:
144+
145+
```ts
146+
import fs from 'node:fs'
147+
import { quansyncMacro, toGenerator } from 'quansync'
148+
149+
// No transformations needed for objects
150+
const readFile = quansyncMacro({
151+
sync: (path: string) => fs.readFileSync(path),
152+
async: (path: string) => fs.promises.readFile(path),
153+
})
154+
155+
// `async function` is transformed into a generator function
156+
const myFunction = quansyncMacro(function* (filename) {
157+
// `await` is transformed into `yield * toGenerator(...)`
158+
const code = yield* toGenerator(readFile(filename, 'utf8'))
159+
160+
return `// some custom prefix\n${code}`
161+
})
162+
```
163+
164+
## Caveats
165+
166+
Both arrow functions and generators have been available since ES2015,
167+
but a "generator arrow function" syntax does not exist
168+
[yet](https://github.com/tc39/proposal-generator-arrow-functions).
169+
170+
You can still use arrow functions with `quansyncMacro`,
171+
but they will be transformed into generator functions,
172+
retaining `this` binding and omitting the `arguments` object.
173+
174+
```ts
175+
const echoNewLine = quansyncMacro(() => 42)
176+
177+
// Transforms to:
178+
179+
const echoNewLine = quansyncMacro((v) => {
180+
return function* () {
181+
return 42
182+
}.call(this)
183+
})
184+
```
185+
186+
To minimize runtime overhead, prioritize using regular functions.
187+
138188
## Sponsors
139189

140190
<p align="center">

eslint.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
import { sxzz } from '@sxzz/eslint-config'
2-
export default sxzz()
2+
export default sxzz({
3+
files: ['README.md/**'],
4+
rules: {
5+
'require-yield': 'off',
6+
},
7+
})

0 commit comments

Comments
 (0)