feat: add await syntax for direct async function calls and new generator/phonebook apps
Add compiler support for calling async functions directly with `await fn(args)` syntax, automatically translating to `await fn.call(args)`. Introduce `create_merge` Makefile target for merging Wren source files. Include new `apps/generator.wren` source code generator using OpenRouter API, `apps/phonebook.wren` CLI phonebook with SQLite backend, and `apps/minitut.md` Wren tutorial reference. Update `example/await_demo.wren` with parameterized async functions, nested awaits, and expression usage. Fix JSON parsing in `example/openrouter_demo.wren` to strip markdown code fences. Document new await syntax in `manual/api/scheduler.html`.
This commit is contained in:
Vendored
+50
@@ -0,0 +1,50 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "scheduler" for Scheduler, Future
|
||||
import "timer" for Timer
|
||||
|
||||
class MockApi {
|
||||
static fetchUser { async { |id|
|
||||
Timer.sleep(1)
|
||||
return {"id": id, "name": "User%(id)", "active": true}
|
||||
}}
|
||||
|
||||
static fetchPosts { async { |userId|
|
||||
Timer.sleep(1)
|
||||
return [
|
||||
{"id": 1, "userId": userId, "title": "Post 1"},
|
||||
{"id": 2, "userId": userId, "title": "Post 2"}
|
||||
]
|
||||
}}
|
||||
|
||||
static createPost { async { |userId, title|
|
||||
Timer.sleep(1)
|
||||
return {"id": 3, "userId": userId, "title": title, "created": true}
|
||||
}}
|
||||
|
||||
static getUserWithPosts(userId) {
|
||||
var fetchUser = MockApi.fetchUser
|
||||
var fetchPosts = MockApi.fetchPosts
|
||||
var user = await fetchUser(userId)
|
||||
var posts = await fetchPosts(userId)
|
||||
|
||||
user["posts"] = posts
|
||||
return user
|
||||
}
|
||||
}
|
||||
|
||||
var fetchUser = MockApi.fetchUser
|
||||
var user = await fetchUser(42)
|
||||
System.print(user["name"]) // expect: User42
|
||||
|
||||
var fetchPosts = MockApi.fetchPosts
|
||||
var posts = await fetchPosts(42)
|
||||
System.print(posts.count) // expect: 2
|
||||
|
||||
var createPost = MockApi.createPost
|
||||
var newPost = await createPost(42, "New Post")
|
||||
System.print(newPost["created"]) // expect: true
|
||||
|
||||
var fullUser = MockApi.getUserWithPosts(99)
|
||||
System.print(fullUser["name"]) // expect: User99
|
||||
System.print(fullUser["posts"].count) // expect: 2
|
||||
Reference in New Issue
Block a user