async — examples
← all topics · 87 examples · page 1 of 2 · raw source ↓
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncIntegrationTests.cs::Async_DotCase_IL_Runs topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
choice Reply {
ok(v: int)
err(msg: string)
}
func replyOf(n: int) -> int {
let v = await Task.FromResult(n)
let r = Reply.ok(v + 1)
match (r: Reply) {
.ok(x) { return x }
.err(_) { return -1 }
}
return -2
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncIntegrationTests.cs::Async_ObjectCreation_IL_Runs topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
data Point { x: int, y: int }
func buildAt(n: int) -> int {
let v = await Task.FromResult(n)
let p = Point { x: v, y: v * 2 }
return p.x + p.y
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncIntegrationTests.cs::Async_StringInterpolation_IL_Runs topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func greet(name: string) -> string {
let hello = await Task.FromResult(name)
return "hi {hello}!"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncIntegrationTests.cs::Async_Throw_IL_SurfacesException topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func failIf(n: int) -> int {
let v = await Task.FromResult(n)
if v < 0 {
throw InvalidOperationException("negative")
}
return v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncIntegrationTests.cs::Async_TryCatch_NoAwaitInTry_IL_Runs topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func safe(n: int) -> int {
let v = await Task.FromResult(n)
var result = 0
try {
result = v * 2
} catch (Exception e) {
result = -1
}
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncIntegrationTests.cs::Async_VoidAwait_IL_Runs topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func delayThenReturn() -> int {
await Task.Delay(1)
return 42
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncIntegrationTests.cs::Async_WhileLoop_IL_Runs topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func sumTo(n: int) -> int {
var i = 1
var total = 0
while i <= n {
let step = await Task.FromResult(i)
total = total + step
i = i + 1
}
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncIntegrationTests.cs::AsyncLet_SimpleTwoPending_IL_Runs topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func runParallel() -> int {
async let a = Task.FromResult(20)
async let b = Task.FromResult(22)
return a + b
}Rejected at compile time: ES3004
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncIntegrationTests.cs::AsyncLet_SyncUserFunc_IL_WrapsInTaskRun topic: async status: verified
// verified behavior: reports diagnostic ES3004
namespace Test
func compute(n: int) -> int {
return n * 10
}
func runBoth() -> int {
async let a = compute(4)
async let b = compute(3)
return a + b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis1_AsyncEvalRefChoice_TryCatchAroundAwait topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
ref choice Expr {
literal(value: int)
add(left: Expr, right: Expr)
fail(reason: string)
}
func eval(e: Expr) -> int {
match (e: Expr) {
.literal(lit) {
let v = await Task.FromResult(lit.value)
return v
}
.add(a) {
let l = eval(a.left)
let r = eval(a.right)
return l + r
}
.fail(f) {
throw InvalidOperationException(f.reason)
}
}
return -1
}
func run() -> int {
let tree = Expr_add { left: Expr_literal { value: 3 }, right: Expr_fail { reason: "no" } }
var result = 0
try {
result = await eval(tree)
} catch (Exception e) {
result = -42
}
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis1_AsyncMatch_AwaitInEveryArm topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
ref choice Cmd {
one(n: int)
two(a: int, b: int)
three(s: string)
}
func handle(c: Cmd) -> int {
match (c: Cmd) {
.one(o) {
let v = await Task.FromResult(o.n)
return v + 1
}
.two(t) {
let a = await Task.FromResult(t.a)
let b = await Task.FromResult(t.b)
return a + b
}
.three(t) {
let s = await Task.FromResult(t.s)
return s.Length
}
}
return -1
}
func one() -> int = await handle(Cmd_one { n: 41 })
func two() -> int = await handle(Cmd_two { a: 10, b: 20 })
func three() -> int = await handle(Cmd_three { s: "hello" })Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis1_AsyncResultMatch_TryCatchInsideArm topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func parseAsync(s: string) -> int {
let v = await Task.FromResult(int.Parse(s))
return v
}
func safeParse(s: string) -> Result<int, string> {
var n = 0
try {
n = await parseAsync(s)
} catch (Exception e) {
return error("bad: {e.Message}")
}
return ok(n)
}
func runOk() -> int {
let r = await safeParse("42")
match (r: Result<int, string>) {
.ok(v) { return v }
.err(_) { return -1 }
}
return -2
}
func runErr() -> int {
let r = await safeParse("nope")
match (r: Result<int, string>) {
.ok(_) { return -1 }
.err(_) { return 99 }
}
return -2
}Runs `run` → 9
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis2_SpawnPtrCapture_ChanCloseInDefer topic: async status: verified
// verified behavior: Test.run(...) == 9
namespace Test
data State {
var produced: int
}
func run() -> int {
var s: *State = new State { produced: 0 }
let ch = chan<int>(4)
let producer = spawn {
defer { ch.Close() }
ch.Send(1)
ch.Send(2)
ch.Send(3)
s.produced = 3
}
producer.Join()
var total = 0
for v in ch {
total += v
}
return total + s.produced
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis7_AsyncLet_FirstError_ShortCircuitsViaQuestion topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func loadAsync(n: int) -> Result<int, string> {
let v = await Task.FromResult(n)
if v < 0 { return error("neg") }
return ok(v)
}
func combine() -> Result<int, string> {
async let a = loadAsync(5)
async let b = loadAsync(0 - 1)
let av = a?
let bv = b?
return ok(av + bv)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis7_AsyncLet_ResultOk_ImplicitAwaitThenUnwrap topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func loadAsync(n: int) -> Result<int, string> {
let v = await Task.FromResult(n)
if v < 0 { return error("neg:{v}") }
return ok(v * 10)
}
func combine() -> Result<int, string> {
async let a = loadAsync(2)
async let b = loadAsync(3)
let av = a?
let bv = b?
return ok(av + bv)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::AsyncIL_EmitsStateMachineStruct topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func fetchValue() -> int {
let result = await Task.FromResult(42)
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::AsyncIL_TaskFromResult_ProducesCorrectValue topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func getValue() -> int {
let result = await Task.FromResult(42)
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::AsyncIL_VoidFunction_ReturnsValueTask topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func doWork() {
await Task.Delay(1)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Await_Transpiler_EmitsAsyncSignature topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func fetchData() -> string {
let result = await Task.FromResult("hello")
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Chan_Buffered_Creation_ReturnsRuntimeChan topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func make() -> Chan<int> {
let ch = chan<int>(4)
return ch
}Runs `run` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Chan_OfUserChoice_ParameterSendFromArg topic: async status: verified
// verified behavior: Test.run(...) == 7
namespace Test
choice Evt {
ping(n: int)
done
}
func run() -> int {
let feed = chan<Evt>(8)
feed.Send(Evt.ping(5))
feed.Close()
return 7
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Chan_SendAndTryReceive_SynchronousRoundTrip topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func echo(x: int) -> bool {
let ch = chan<int>(1)
ch.Send(x)
return true
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Chan_Unbuffered_Creation_DefaultsToZeroCapacity topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func make() -> Chan<string> {
let ch = chan<string>(0)
return ch
}Runs `run` → 6
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Spawn_CallsHelperWithCapturedChan topic: async status: verified
// verified behavior: Test.run(...) == 6
namespace Test
func send3(ch: chan<int>) {
ch.Send(1)
ch.Send(2)
ch.Send(3)
ch.Close()
}
func run() -> int {
let ch = chan<int>(4)
let producer = spawn {
send3(ch)
}
producer.Join()
var total = 0
for v in ch {
total += v
}
return total
}Runs `run` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Spawn_CapturesChanOfUserChoice_CallsHelper topic: async status: verified
// verified behavior: Test.run(...) == 7
namespace Test
choice Evt {
ping(n: int)
done
}
func produce(ch: chan<Evt>) {
ch.Send(Evt.ping(1))
ch.Send(Evt.ping(2))
ch.Close()
}
func run() -> int {
let feed = chan<Evt>(8)
let job = spawn {
produce(feed)
}
job.Join()
return 7
}Runs `run` → 6
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Spawn_WithChanCapture_ProducerConsumer topic: async status: verified
// verified behavior: Test.run(...) == 6
namespace Test
func run() -> int {
let ch = chan<int>(4)
let producer = spawn {
ch.Send(1)
ch.Send(2)
ch.Send(3)
ch.Close()
}
producer.Join()
var total = 0
for v in ch {
total += v
}
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::ExternalAwaitable_TaskFromResult topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
func combine() -> int {
async let a = Task.FromResult(20)
async let b = Task.FromResult(22)
return a + b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::PlainAwait_AsyncUserFn topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::QuestionPropagation_InAsync_ErrorPath topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func loadAsync(n: int) -> Result<int, string> {
let v = await Task.FromResult(n)
if v < 0 { return error("neg") }
return ok(v * 10)
}
func run() -> Result<int, string> {
let r = await loadAsync(0 - 5)
let v = r?
return ok(v + 1)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::QuestionPropagation_InAsync_OkPath topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func loadAsync(n: int) -> Result<int, string> {
let v = await Task.FromResult(n)
if v < 0 { return error("neg") }
return ok(v * 10)
}
func run() -> Result<int, string> {
let r = await loadAsync(4)
let v = r?
return ok(v + 1)
}Runs `sum` → 15
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::Added_AwaitArithmeticResult topic: async status: verified
// verified behavior: Test.sum(...) == 15
namespace Test
func sum() -> int {
let a = await Task.FromResult(5)
let b = await Task.FromResult(10)
return a + b
}Runs `pick` → 1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::Added_AwaitInsideIf topic: async status: verified
// verified behavior: Test.pick(...) == 1
namespace Test
func pick(b: bool) -> int {
if b {
let x = await Task.FromResult(1)
return x
}
return 0
}Runs `b` → 12
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::Added_Default_AwaitChain_Value topic: async status: verified
// verified behavior: Test.b(...) == 12
namespace Test
func a() -> int { let x = await Task.FromResult(10) return x }
func b() -> int { let y = await a() return y + 2 }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::Added_DefaultAsync_IsValueTask topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func compute() -> int {
let x = await Task.FromResult(40)
return x + 2
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::Added_ExplicitVoid_AsyncVoid topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func handler() -> void {
await Task.Delay(1)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::Added_NonGenericTask_Void topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func run() -> Task {
await Task.Delay(1)
}Runs `use` → 8
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::Added_TaskOfT_AwaitedToValue topic: async status: verified
// verified behavior: Test.use(...) == 8
namespace Test
func get() -> Task<int> {
let x = await Task.FromResult(8)
return x
}
func use() -> int {
let v = await get()
return v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::Added_TaskOfT_ReturnsTaskType topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func fetch() -> Task<int> {
let x = await Task.FromResult(7)
return x
}Runs `range` → 10
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::AsyncStream_Parameterized_LoopYield_Streams topic: async status: verified
// verified behavior: Test.range(...) == 10
namespace Test
func range(n: int) -> IAsyncEnumerable<int> {
var i = 0
while i < n {
yield i
i += 1
}
}Runs `nums` → 141
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::AsyncStream_YieldAndAwait_Streams topic: async status: verified
// verified behavior: Test.nums(...) == 141
namespace Test
func nums() -> IAsyncEnumerable<int> {
yield 1
let x = await Task.FromResult(40)
yield x
yield 100
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::ExplicitVoid_Await_EmitsAsyncVoid topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func handler() -> void {
let _ = await Task.FromResult(0)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::OmittedReturn_Await_StaysValueTask topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func handler() {
let _ = await Task.FromResult(0)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::Task_Void_EmitsTaskReturn topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func ping() -> Task {
let _ = await Task.FromResult(0)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::TaskOfT_EmitsTaskReturn topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func compute() -> Task<int> {
let x = await Task.FromResult(40)
return x + 2
}Runs `two` → 10
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::TaskOfT_RunsAndComposesWithBcl topic: async status: verified
// verified behavior: Test.two(...) == 10
namespace Test
func one() -> Task<int> {
let x = await Task.FromResult(4)
return x + 1
}
func two() -> Task<int> {
let y = await one()
return y + 5
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncParity.cs::ValueTaskOfT_Explicit_EmitsValueTask topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func compute() -> ValueTask<int> {
let x = await Task.FromResult(7)
return x
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::AsyncLet_Fanout topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
async let a = Task.FromResult(20)
async let b = Task.FromResult(22)
return a + b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Await_TaskFromResult topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let v = await Task.FromResult(42)
return v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Await_TwoSequential topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let a = await Task.FromResult(10)
let b = await Task.FromResult(20)
return a + b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::Spawn_ChannelProducerConsumer topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let ch = chan<int>(8)
let producer = spawn {
ch.Send(1)
ch.Send(2)
ch.Send(3)
ch.Close()
}
producer.Wait()
var total = 0
for v in ch {
total += v
}
return total
}