@@ -135,6 +135,56 @@ const asyncResult = await myFunction.async('./some-file.js')
135
135
136
136
For more details on usage, refer to [ quansync's docs] ( https://github.com/antfu-collective/quansync#usage ) .
137
137
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
+
138
188
## Sponsors
139
189
140
190
<p align =" center " >
0 commit comments