Skip to content

async — examples

← all topics · 126 examples · page 2 of 3 · raw source ↓

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)
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::SyncUserFn_AsyncLet_Runs   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func compute(n: int) -> int = n * 2

func combine() -> int {
    async let a = compute(4)
    async let b = compute(3)
    return a + b
}

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
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Misc.cs::TaskFunc_SpawnAndWait   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
task func produce() -> int {
    return 42
}
func go() -> int {
    let job = produce()
    return job.Wait()
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::B07_GenericRefData_IAsyncDisposable   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
using "System"
using "System.Threading.Tasks"
class Res<T> : IAsyncDisposable {
    v: T
    init(x: T) { self.v = x }
    func DisposeAsync() -> ValueTask {
        await Task.CompletedTask
    }
}
func make() -> Res<int> = Res<int>(1)

Runs `null` → 8

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::B09_GenericRefData_AsyncMethodReturnsTaskOfT   topic: async   status: verified
// verified behavior: Test.null(...) == 8

namespace Test
using "System.Threading.Tasks"
class Ch<T> {
    v: T
    init(x: T) { self.v = x }
    func receiveAsync() -> Task<T> {
        await Task.Delay(1)
        return self.v
    }
}
func make() -> Ch<int> = Ch<int>(8)

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::D04_RefData_IAsyncDisposable_AwaitInBody   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
using "System"
using "System.Threading.Tasks"
class Scope : IAsyncDisposable {
    var closed: bool
    init() { self.closed = false }
    func DisposeAsync() -> ValueTask {
        await Task.Delay(1)
        self.closed = true
    }
}
func make() -> Scope = Scope()

Runs `null` → 13

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Body_Computes_Arithmetic   topic: async   status: verified
// verified behavior: Test.null(...) == 13

namespace Test

task func sum() -> int {
    let a = 3
    let b = 4
    return a * b + 1
}

Runs `caller` → 5

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Call_Site_Returns_Job_Typed_Result_To_Caller   topic: async   status: verified
// verified behavior: Test.caller(...) == 5

namespace Test

task func produce() -> int { return 5 }

func caller() -> int {
    let j = produce()
    return j.Wait()
}

Runs `caller` → 11

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Captures_Let_Across_Boundary_Compiles   topic: async   status: verified
// verified behavior: Test.caller(...) == 11

namespace Test

task func produce() -> int {
    let x = 11
    return x
}

func caller() -> int { return produce().Wait() }

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Job_Cancel_Available   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

task func produce() -> int { return 1 }

Runs `caller` → 60

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Multiple_Awaits_Sum_To_Expected   topic: async   status: verified
// verified behavior: Test.caller(...) == 60

namespace Test

task func first() -> int { return 10 }
task func second() -> int { return 20 }
task func third() -> int { return 30 }

func caller() -> int = first().Wait() + second().Wait() + third().Wait()

Runs `caller` → 5

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Multiple_TaskFuncs_In_Same_File   topic: async   status: verified
// verified behavior: Test.caller(...) == 5

namespace Test

task func two() -> int { return 2 }
task func three() -> int { return 3 }

func caller() -> int { return two().Wait() + three().Wait() }

Runs `null` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Returns_Boolean_Job   topic: async   status: verified
// verified behavior: Test.null(...) == true

namespace Test

task func check() -> bool { return true }

Runs `null` → "ok"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Returns_String_Job   topic: async   status: verified
// verified behavior: Test.null(...) == "ok"

namespace Test

task func tag() -> string { return "ok" }

Runs `caller` → 300

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Two_Independent_Tasks_Wait_Independently   topic: async   status: verified
// verified behavior: Test.caller(...) == 300

namespace Test

task func one() -> int { return 100 }
task func two() -> int { return 200 }

func caller() -> int = one().Wait() + two().Wait()

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Typed_Parses_And_Emits_JobT_Return   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

task func produce() -> int { return 42 }

Rejected at compile time: ES2130

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Var_Capture_In_Function_Literal_Reports_ES2130   topic: async   status: verified
// verified behavior: reports diagnostic ES2130

namespace Test

task func go() {
    var counter = 0
    let bump = func() { counter = counter + 1 }
    bump()
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Void_Returns_Job_That_Completes   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

task func ping() { }

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Wrapper_Calls_Job_Spawn   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

task func produce() -> int { return 7 }

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Wrapper_Invokes_Inner   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

task func produce() -> int { return 99 }

Runs `gap5a` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::AsyncCodegen_AwaitBareTask_InNonVoidAsync   topic: async   status: verified
// verified behavior: Test.gap5a(...) == 7


namespace Test
func bare() -> Task { await Task.Delay(1) }
func gap5a() -> int {
    await bare()
    return 7
}

Runs `gap5bbranch` → 102

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::AsyncCodegen_AwaitInForeachBranch   topic: async   status: verified
// verified behavior: Test.gap5bbranch(...) == 102


namespace Test
func gap5bbranch() -> int {
    let xs = List<int>()
    xs.Add(1)
    xs.Add(2)
    var total = 0
    for x in xs {
        if x == 1 {
            total += 100
        } else {
            await Task.Delay(1)
            total += x
        }
    }
    return total
}

Runs `gap5bptr` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::AsyncCodegen_AwaitInForeachOverPointerList   topic: async   status: verified
// verified behavior: Test.gap5bptr(...) == 7


namespace Test
struct Box { var v: int }
func gap5bptr() -> int {
    let xs = List<*Box>()
    xs.Add(new Box { v: 2 })
    xs.Add(new Box { v: 5 })
    var total = 0
    for b in xs {
        await Task.Delay(1)
        total += b.v
    }
    return total
}

Runs `gap5btry` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::AsyncCodegen_AwaitInForeachWithTryCatch   topic: async   status: verified
// verified behavior: Test.gap5btry(...) == 3


namespace Test
func gap5btry() -> int {
    let xs = List<int>()
    xs.Add(1)
    xs.Add(2)
    var total = 0
    for x in xs {
        try {
            await Task.Delay(1)
            total += x
        } catch {
            total += 100
        }
    }
    return total
}

Runs `gap5bnested` → 9

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::AsyncCodegen_AwaitInNestedForeach   topic: async   status: verified
// verified behavior: Test.gap5bnested(...) == 9


namespace Test
func gap5bnested() -> int {
    let xs = List<int>()
    xs.Add(1)
    xs.Add(2)
    var total = 0
    for x in xs {
        for y in xs {
            await Task.Delay(1)
            total += x * y
        }
    }
    return total
}

Runs `gap5brange` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::AsyncCodegen_AwaitInRangeLoop   topic: async   status: verified
// verified behavior: Test.gap5brange(...) == 3


namespace Test
func gap5brange() -> int {
    var total = 0
    for i in 0..3 {
        await Task.Delay(1)
        total += i
    }
    return total
}

Runs `gap5bfor` → 6

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::AsyncCodegen_AwaitInsideForeach   topic: async   status: verified
// verified behavior: Test.gap5bfor(...) == 6


namespace Test
func gap5bfor() -> int {
    let xs = List<int>()
    xs.Add(1)
    xs.Add(2)
    xs.Add(3)
    var total = 0
    for x in xs {
        await Task.Delay(1)
        total += x
    }
    return total
}

Runs `gap5b` → 21

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::AsyncCodegen_AwaitInsideLoopBranch_NoBranchIntoTry   topic: async   status: verified
// verified behavior: Test.gap5b(...) == 21


namespace Test
func gap5b() -> int {
    var total = 0
    var i = 0
    while i < 3 {
        if i == 1 {
            total += 1
        } else {
            await Task.Delay(1)
            total += 10
        }
        i += 1
    }
    return total
}

Runs `gapselfcompound` → 5

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::AsyncCodegen_CompoundAssignFieldOnClassSelf   topic: async   status: verified
// verified behavior: Test.gapselfcompound(...) == 5


namespace Test
class Counter {
    var n: int
    init() { self.n = 0 }
    func bump() -> Task {
        await Task.Delay(1)
        self.n += 5
    }
}
func gapselfcompound() -> int {
    let c = Counter()
    let t = c.bump()
    t.Wait()
    return c.n
}