Source: src/hql/transpiler/pipeline/transform/async-generators.ts
HQL provides first-class support for asynchronous programming and generators, mapping directly to JavaScript's async/await and function*/yield constructs.
(async fn name [params] body...) -- compiles to async function(await expr) -- wraps in __hql_consume_async_iter for async iterator support(fn* name [params] body...) -- compiles to function*(yield value) or (yield) for undefined(yield* iterable) -- delegates to another iterator(async fn* name [params] body...) -- combines both(for-await-of [item iterable] body...) -- async iteration;; Async function
(async fn fetch-user [id]
(let response (await (js/fetch (+ "/api/users/" id))))
(await (.json response)))
;; Generator function
(fn* range [start end]
(var i start)
(while (< i end)
(yield i)
(= i (+ i 1))))
;; Consume generator
(for-of [n (range 1 6)]
(print n)) ;; prints 1 2 3 4 5
;; Async generator
(async fn* stream-pages [url]
(var page 1)
(while true
(let data (await (fetch-page url page)))
(if (isEmpty data) (return))
(yield data)
(= page (+ page 1))))