You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: add async examples and clarify queueMicrotask
Add code examples for async function translation based on the
async_basic integration test.
Clarify that `go` statements are translated using `queueMicrotask`.
Signed-off-by: Christian Stewart <christian@aperture.us>
Copy file name to clipboardExpand all lines: design/DESIGN.md
+61Lines changed: 61 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -487,12 +487,73 @@ To determine which functions need to be marked `async` in TypeScript, the compil
487
487
488
488
### TypeScriptGeneration
489
489
490
+
## Functions
491
+
490
492
- **Async Functions:** Go functions colored as **Asynchronous** are generated as TypeScript`async function`s. Theirreturntype`T` is wrapped in a `Promise<T>`. If the function has no return value, the TypeScriptreturntype is `Promise<void>`.
491
493
- **Sync Functions:** Go functions colored as **Synchronous** are generated as regular TypeScript`function`s with their corresponding return types.
492
494
- **Function Calls:** When a Go function call targets an **Asynchronous** function, the generated TypeScript call expression is prefixed with the `await` keyword. Calls to **Synchronous** functions are generated directly without `await`.
493
495
494
496
This coloring approach ensures that asynchronous operations propagate correctly through the call stack in the generated TypeScript code.
495
497
498
+
### AsyncExample
499
+
500
+
Consider the following Go code using a channel:
501
+
502
+
```go
503
+
package main
504
+
505
+
// This function receives from a channel, making it async.
506
+
func receiveFromChan(ch chan int) int {
507
+
val := <-ch // This operation makes the function async
508
+
return val
509
+
}
510
+
511
+
// This function calls an async function, making it async too.
512
+
func caller(ch chan int) int {
513
+
// We expect this call to be awaited in TypeScript
514
+
result := receiveFromChan(ch)
515
+
return result + 1
516
+
}
517
+
518
+
func main() {
519
+
myChan := make(chan int, 1)
520
+
myChan <- 10
521
+
finalResult := caller(myChan)
522
+
println(finalResult) // Expected output: 11
523
+
close(myChan)
524
+
}
525
+
526
+
```
527
+
528
+
This translates to the following TypeScript:
529
+
530
+
```typescript
531
+
import * as goscript from "@go/builtin";
532
+
533
+
// Marked async because it contains 'await ch.receive()'
534
+
async function receiveFromChan(ch: goscript.Channel<number>): Promise<number> {
535
+
let val = await ch.receive()
536
+
return val
537
+
}
538
+
539
+
// Marked async because it calls the async 'receiveFromChan'
540
+
async function caller(ch: goscript.Channel<number>): Promise<number> {
541
+
let result = await receiveFromChan(ch) // Call is awaited
542
+
return result + 1
543
+
}
544
+
545
+
// Marked async because it calls the async 'caller' and uses 'await myChan.send()'
546
+
export async function main(): Promise<void> {
547
+
let myChan = goscript.makeChannel<number>(1)
548
+
await myChan.send(10) // Send is awaited
549
+
let finalResult = await caller(myChan) // Call is awaited
550
+
console.log(finalResult)
551
+
myChan.close()
552
+
}
553
+
```
554
+
555
+
*Note on Microtasks:* WhileGo's concurrency model involves goroutines and a scheduler, the TypeScript translation primarily uses `async`/`await` and Promises for channel operations. Starting a new Goroutine with the `go` keyword is translated to a call to `queueMicrotask` with the target function, effectively scheduling it to run asynchronously similar to how Promises resolve.
556
+
496
557
## Constants
497
558
498
559
Go `const` declarations are translated into TypeScript `let` declarations at the module scope.
0 commit comments