async — examples
← all topics · 126 examples · page 1 of 3 · 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
union 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
struct 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: AsyncSpillTests.cs::AndAlso_ShortCircuit_DoesNotAwaitRight topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Log { var v: int }
func aflag(l: *Log) -> bool {
l.v = 99
return true
}
func go() -> int {
var l: *Log = new Log { v: 0 }
let b = false && (await Task.FromResult(aflag(l)))
return l.v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::AndAlso_TrueLeft_RunsRight topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Log { var v: int }
func aflag(l: *Log) -> bool {
l.v = 99
return true
}
func go() -> int {
var l: *Log = new Log { v: 0 }
let b = true && (await Task.FromResult(aflag(l)))
return l.v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::AwaitAsBinaryRightOperand_Compiles_AndComputes topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func asy(n: int) -> int {
return await Task.FromResult(n + 1)
}
func go() -> int {
var acc = 1
acc = acc * (await asy(acc))
return acc
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::AwaitInInterpolationHole topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> string {
let n = await Task.FromResult(42)
return "value={await Task.FromResult(n)}!"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::AwaitInListLiteral topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let xs = [await Task.FromResult(1), 2, await Task.FromResult(3)]
var total = 0
for x in xs { total += x }
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::AwaitInTryBody_NestedInExpression topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var acc = 1
try {
acc = acc + (await Task.FromResult(5))
} catch (Exception e) {
acc = -1
}
return acc
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::CompoundAssignment_ReadsTargetBeforeAwait topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Cell { var n: int }
func bump(c: *Cell) -> int {
c.n = 100
return 7
}
func go() -> int {
var c: *Cell = new Cell { n: 5 }
c.n += await Task.FromResult(bump(c))
return c.n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::Conditional_AwaitInBranches_OnlyTakenRuns topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let cond = true
let r = cond ? (await Task.FromResult(7)) : (await Task.FromResult(13))
return r
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::DefiniteReturn_ThroughRewrittenConditional topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let cond = true
return cond ? (await Task.FromResult(7)) : 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::EvaluationOrder_CallArgsAroundAwait topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Log { var v: int }
func g(l: *Log) -> int {
l.v = l.v * 10 + 1
return 1
}
func h(l: *Log) -> int {
l.v = l.v * 10 + 2
return 2
}
func k(l: *Log) -> int {
l.v = l.v * 10 + 3
return 3
}
func sum3(a: int, b: int, c: int) -> int {
return a + b + c
}
func go() -> int {
var l: *Log = new Log { v: 0 }
let r = sum3(g(l), await Task.FromResult(h(l)), k(l))
return l.v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::EvaluationOrder_LeftOperandReadBeforeAwait topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Log { var v: int }
func g(l: *Log) -> int {
l.v = l.v * 10 + 1
return 1
}
func h(l: *Log) -> int {
l.v = l.v * 10 + 2
return 2
}
func go() -> int {
var l: *Log = new Log { v: 0 }
let r = g(l) + (await Task.FromResult(h(l)))
return l.v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::ForEachCollectionAwait topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
var total = 0
for x in (await Task.FromResult([1, 2, 3])) {
total += x
}
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::LetElse_AwaitInInitializer topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func lookup(present: bool) -> int? {
if present { return 10 }
return nil
}
func go() -> int {
let v = lookup(true) else { return -1 }
let w = v + (await Task.FromResult(10))
return w
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::MemberTargetAssignment_WithAwaitValue topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Cell { var n: int }
func go() -> int {
var c: *Cell = new Cell { n: 0 }
c.n = await Task.FromResult(9)
return c.n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::MultipleAwaitsInArithmetic topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
return (await Task.FromResult(1)) + (await Task.FromResult(2)) * (await Task.FromResult(3))
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::NestedAwaits topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func inc(n: int) -> int { return await Task.FromResult(n + 1) }
func go() -> int {
return await inc(await inc(6))
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::NullCoalescing_AwaitInRight_OnlyWhenLeftNil topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func maybe(present: bool) -> int? {
if present { return 5 }
return nil
}
func go() -> int {
let r = maybe(true) ?? (await Task.FromResult(99))
return r
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::OrElse_ShortCircuit_DoesNotAwaitRight topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Log { var v: int }
func aflag(l: *Log) -> bool {
l.v = 99
return false
}
func go() -> int {
var l: *Log = new Log { v: 0 }
let b = true || (await Task.FromResult(aflag(l)))
return l.v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::SyncExpressionInAsyncBody_Unchanged topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int {
let a = 10 * 2
let b = await Task.FromResult(10)
return a + b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: AsyncSpillTests.cs::WhileConditionAwait_ReEvaluatesEachIteration topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Counter { var n: int }
func more(c: *Counter) -> bool { return c.n < 5 }
func go() -> int {
var c: *Counter = new Counter { n: 0 }
while await Task.FromResult(more(c)) {
c.n = c.n + 1
continue
}
return c.n
}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 union 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 union 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
struct 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
union 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` → 99
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Select_WithDefault_NoneReady_TakesDefault topic: async status: verified
// verified behavior: Test.run(...) == 99
namespace Test
func run() -> int {
let ch = chan<int>(1)
var fired = 0
select {
.recv(v, ch) { fired = 1 }
default { fired = 99 }
}
return fired
}Runs `run` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Select_WithDefault_OneReady_TakesThatArm topic: async status: verified
// verified behavior: Test.run(...) == 42
namespace Test
func run() -> int {
let ch = chan<int>(1)
ch.Send(42)
var got = 0
select {
.recv(v, ch) { got = v }
default { got = 99 }
}
return got
}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
union 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