Skip to content

Commit 952accc

Browse files
committed
feat: add async/await support and error handling for I/O operations in README and main.mbt
- Introduced async support for `readFile`, `readDir`, `exec`, and `fetch` functions. - Updated README with examples demonstrating async usage and error handling. - Modified `main.mbt` to reflect new async examples for `exec` and `fetch` operations.
1 parent 227fdb5 commit 952accc

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

README.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,66 @@ A web framework for MoonBit.
1010

1111
Minimum Example: https://github.com/oboard/mocket_example
1212

13+
## Async Support
14+
15+
The library now supports async/await for I/O operations:
16+
17+
- `readFile`: Asynchronously read files
18+
- `readDir`: Asynchronously read directories
19+
- `exec`: Asynchronously execute commands
20+
- `fetch`: Asynchronously make HTTP requests
21+
22+
Async functions can be called using the `!!` operator and must be wrapped in
23+
`run_async` when called from synchronous contexts.
24+
25+
```moonbit
26+
run_async(fn() {
27+
try {
28+
let result = @mocket.exec!!("ls")
29+
println(result)
30+
} catch {
31+
err => println("Error executing command: \{err}")
32+
}
33+
})
34+
run_async(fn() {
35+
try {
36+
let response = @mocket.fetch!!("https://api64.ipify.org/")
37+
println(response)
38+
} catch {
39+
err => println("Error fetching data: \{err}")
40+
}
41+
})
42+
```
43+
44+
## Error Handling
45+
46+
Async operations return `Result` types or use the `!` error type syntax:
47+
48+
- `readFile`: Returns `Bytes!Error`
49+
- `exec`: Returns `String!Error`
50+
- `fetch`: Returns `FetchResponse!NetworkError`
51+
52+
Use `try/catch` blocks to handle potential errors.
53+
1354
### MocketGo Runtime (Experimental)
14-
Download the latest release from:
15-
https://github.com/oboard/mocketgo/releases
55+
56+
Download the latest release from: https://github.com/oboard/mocketgo/releases
57+
1658
#### Linux/MacOS:
59+
1760
```bash
1861
chmod +x ./mocketgo
1962
./mocketgo main.wasm
2063
```
2164

2265
#### Windows:
66+
2367
```bat
2468
mocketgo.exe main.wasm
2569
```
2670

2771
### Node.js Runtime
72+
2873
#### Prerequisites
2974

3075
- MoonBit SDK installed
@@ -51,7 +96,7 @@ start.bat
5196

5297
## Example usage
5398

54-
```rust
99+
```moonbit
55100
// Example usage of mocket package in MoonBit
56101
57102
fn main {
@@ -120,5 +165,4 @@ fn main {
120165
// Example: http://localhost:4000/static/logo.jpg => ./logo.jpg
121166
server.static("/static/", "./")
122167
}
123-
124168
```

src/main/main.mbt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ fn main {
8484
// static file serving example
8585
// Example: http://localhost:4000/static/logo.jpg => ./logo.jpg
8686
server.resource("/static/", "./")
87+
8788
run_async(fn() {
88-
// 修改exec示例
8989
try {
9090
let result = @mocket.exec!!("ls")
9191
println(result)
@@ -94,7 +94,6 @@ fn main {
9494
}
9595
})
9696
run_async(fn() {
97-
// 修改fetch示例
9897
try {
9998
let response = @mocket.fetch!!("https://api64.ipify.org/")
10099
println(response)

0 commit comments

Comments
 (0)