-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
36 lines (33 loc) · 901 Bytes
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { behavioralThreadSystem } from "./bthreads.ts"
import { main, sleep } from "effection"
async function example() {
await main(() =>
behavioralThreadSystem(function* (thread, sync) {
// Worker thread with timeout
yield* thread("worker", function* () {
const result = yield sync({
wait: (e) => e === "timeout", // Can be interrupted by timeout
exec: function* () {
// Async operation to attempt
yield* sleep(2000)
return "completed"
},
})
console.log("worker done", result)
})
// Timer thread
yield* thread("timer", function* () {
yield sync({
exec: function* () {
yield* sleep(1000)
return "timeout"
},
})
console.log("timer done")
})
})
)
}
if (import.meta.main) {
await example()
}