Skip to content

Commit 3262a02

Browse files
committed
docs: Add real-world async examples to async runtime chapter
Added 5 comprehensive examples demonstrating working async patterns: 1. Async Loops with State - sum_range with while loops 2. Await Inside Loops - sum_doubled awaiting in each iteration 3. Chained Async Calls - step1 -> step2 -> step3 pipeline 4. Long-Running Process with Await - add_to_sum awaiting long_sum 5. Multiple Parameters - sum_with_multiplier with 3 params All examples are based on passing test cases and demonstrate the state machine's ability to handle: - Mutable loop variables (var) - Nested Promise polling - Cross-state value capture - Multiple function parameters
1 parent 911f093 commit 3262a02

1 file changed

Lines changed: 160 additions & 1 deletion

File tree

book/13-async-runtime.md

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,166 @@ let tier = runtime.get_function_tier("hot_async")?;
319319
println!("Optimization tier: {:?}", tier);
320320
```
321321

322-
## Example: Async Computation Pipeline
322+
## Real-World Examples
323+
324+
### Example 1: Async Loops with State
325+
326+
Async functions can contain while loops with mutable state. The state machine correctly captures and restores loop variables across poll boundaries:
327+
328+
```zig
329+
async fn sum_range(n: i32) i32 {
330+
var total: i32 = 0;
331+
var i: i32 = 1;
332+
while (i <= n) {
333+
total = total + i;
334+
i = i + 1;
335+
}
336+
return total;
337+
}
338+
```
339+
340+
```rust
341+
let promise = runtime.call_async("sum_range", &[ZyntaxValue::Int(100)])?;
342+
343+
// Poll until completion
344+
while promise.is_pending() {
345+
promise.poll();
346+
}
347+
348+
// sum_range(100) = 1+2+...+100 = 5050
349+
assert_eq!(promise.state(), PromiseState::Ready(ZyntaxValue::Int(5050)));
350+
```
351+
352+
### Example 2: Await Inside Loops
353+
354+
A powerful pattern is awaiting other async functions inside loops. Each iteration creates a new nested Promise:
355+
356+
```zig
357+
async fn double(x: i32) i32 {
358+
return x * 2;
359+
}
360+
361+
async fn sum_doubled(n: i32) i32 {
362+
var total: i32 = 0;
363+
var i: i32 = 1;
364+
while (i <= n) {
365+
const doubled = await double(i); // Await in loop!
366+
total = total + doubled;
367+
i = i + 1;
368+
}
369+
return total;
370+
}
371+
```
372+
373+
```rust
374+
let promise = runtime.call_async("sum_doubled", &[ZyntaxValue::Int(5)])?;
375+
376+
while promise.is_pending() {
377+
promise.poll();
378+
}
379+
380+
// sum_doubled(5) = double(1) + double(2) + ... + double(5)
381+
// = 2 + 4 + 6 + 8 + 10 = 30
382+
assert_eq!(promise.state(), PromiseState::Ready(ZyntaxValue::Int(30)));
383+
```
384+
385+
### Example 3: Chained Async Calls
386+
387+
Async functions can await other async functions and perform additional computation:
388+
389+
```zig
390+
async fn step1(x: i32) i32 {
391+
return x + 10;
392+
}
393+
394+
async fn step2(x: i32) i32 {
395+
const result = await step1(x);
396+
return result * 2;
397+
}
398+
399+
async fn step3(x: i32) i32 {
400+
const result = await step2(x);
401+
return result + 5;
402+
}
403+
```
404+
405+
```rust
406+
let promise = runtime.call_async("step3", &[ZyntaxValue::Int(5)])?;
407+
408+
while promise.is_pending() {
409+
promise.poll();
410+
}
411+
412+
// step3(5) = step2(5) + 5 = (step1(5) * 2) + 5 = ((5+10) * 2) + 5 = 35
413+
assert_eq!(promise.state(), PromiseState::Ready(ZyntaxValue::Int(35)));
414+
```
415+
416+
### Example 4: Long-Running Process with Await
417+
418+
Await a long-running async function and process the result:
419+
420+
```zig
421+
// A long-running async process that sums 1 to n
422+
async fn long_sum(n: i32) i32 {
423+
var total: i32 = 0;
424+
var i: i32 = 1;
425+
while (i <= n) {
426+
total = total + i;
427+
i = i + 1;
428+
}
429+
return total;
430+
}
431+
432+
// Awaits the long-running process and adds a constant
433+
async fn add_to_sum(n: i32) i32 {
434+
const sum = await long_sum(n);
435+
return sum + 100;
436+
}
437+
```
438+
439+
```rust
440+
let promise = runtime.call_async("add_to_sum", &[ZyntaxValue::Int(50)])?;
441+
442+
while promise.is_pending() {
443+
promise.poll();
444+
}
445+
446+
// add_to_sum(50) = long_sum(50) + 100 = 1275 + 100 = 1375
447+
assert_eq!(promise.state(), PromiseState::Ready(ZyntaxValue::Int(1375)));
448+
```
449+
450+
### Example 5: Multiple Parameters
451+
452+
Async functions handle multiple parameters correctly:
453+
454+
```zig
455+
async fn sum_with_multiplier(start: i32, end: i32, multiplier: i32) i32 {
456+
var total: i32 = 0;
457+
var i: i32 = start;
458+
while (i <= end) {
459+
total = total + (i * multiplier);
460+
i = i + 1;
461+
}
462+
return total;
463+
}
464+
```
465+
466+
```rust
467+
let promise = runtime.call_async("sum_with_multiplier", &[
468+
ZyntaxValue::Int(1),
469+
ZyntaxValue::Int(5),
470+
ZyntaxValue::Int(2),
471+
])?;
472+
473+
while promise.is_pending() {
474+
promise.poll();
475+
}
476+
477+
// sum_with_multiplier(1, 5, 2) = (1*2)+(2*2)+(3*2)+(4*2)+(5*2) = 30
478+
assert_eq!(promise.state(), PromiseState::Ready(ZyntaxValue::Int(30)));
479+
```
480+
481+
## Async Computation Pipeline (Legacy Example)
323482

324483
```rust
325484
use zyntax_embed::{ZyntaxRuntime, LanguageGrammar, ZyntaxValue};

0 commit comments

Comments
 (0)