async — examples
← all topics · 87 examples · page 2 of 2 · raw source ↓
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()
}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
}ILEmitterTests_TaskFunc__TaskFunc_Call_Site_Returns_Job_Typed_Result_To_Caller
async runnable verifiedRuns `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" }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Returns_Synonym_Also_Works topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
task func produce() returns int { return 7 }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 }ILEmitterTests_TaskFunc__TaskFunc_Var_Capture_In_Function_Literal_Reports_ES2130
async negative verifiedRejected 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 }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_Chan_Creation topic: async status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Audit
pub func makeChan() -> Chan<string> {
let ch = chan<string>(256)
return ch
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: EmitterStressTests.cs::AsyncWithTupleReturnAndForDestructuring topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func work() -> (List<string>, List<string>) {
var items = List<string>()
var errors = List<string>()
return (items, errors)
}
func run() {
var tasks = List<Task<(List<string>, List<string>)>>()
tasks.Add(Task.Run(func() -> (List<string>, List<string>) { return work() }))
let results = await Task.WhenAll(tasks.ToArray())
for (items, errors) in results {
let n = items.Count + errors.Count
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: EmitterStressTests.cs::RefDataWithAsyncMethodAndTryCatch topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace T
data Item(title: string, source: string)
func fetchItems(client: HttpClient, url: string) -> (List<Item>, List<string>) {
var items = List<Item>()
var errors = List<string>()
try {
let stream = await client.GetStreamAsync(url)
items.Add(Item("title", "src"))
} catch (Exception ex) {
errors.Add(ex.Message)
}
return (items, errors)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: EmitterStressTests.cs::RefDataWithMultipleMethodsAndExpressionBodies topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace T
data Config(name: string, url: string)
data Item(title: string, source: string)
data Status(name: string, itemCount: int)
ref data Svc {
configs: List<Config>
items: List<Item>
statuses: List<Status>
client: HttpClient
maxItems: int
init() {
self.configs = List<Config>()
self.items = List<Item>()
self.statuses = List<Status>()
self.client = HttpClient()
self.maxItems = 500
}
func poll() {
var tasks = List<Task<(List<Item>, List<string>)>>()
for cfg in self.configs {
let c = self.client
let f = cfg
tasks.Add(Task.Run(func() -> (List<Item>, List<string>) { return (List<Item>(), List<string>()) }))
}
let results = await Task.WhenAll(tasks.ToArray())
var all = List<Item>()
var statuses = List<Status>()
var i = 0
for (items, errors) in results {
let cfg = self.configs[i]
i += 1
let err = errors.Count > 0 ? errors[0] : ""
statuses.Add(Status(cfg.name, items.Count))
all.AddRange(items)
}
if all.Count > self.maxItems {
all.RemoveRange(self.maxItems, all.Count - self.maxItems)
}
self.items = all
self.statuses = statuses
}
func count() -> int = self.items.Count
func addConfig(name: string, url: string) = self.configs.Add(Config(name, url))
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis1_AsyncResultMatch_BadPattern_DiagnosesUnawaitedAsyncCall topic: async status: unverified
// 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> {
let n = parseAsync(s)
return ok(n)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Await_Binder_SetsHasAwait topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func fetch() -> string {
let x = await someCall()
return x
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Await_Parser_ParsesAwaitExpression topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func fetch() -> string {
let x = await someTask()
return x
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Emit_AsyncFunctionWithAwait topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func fetch() -> Task<string> {
let client = HttpClient()
let result = await client.GetStringAsync("http://example.com")
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Emit_ComplexRefDataWithAsyncAndLambdas topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func work() -> (List<string>, List<string>) {
var items = List<string>()
var errors = List<string>()
return (items, errors)
}
func run() {
var tasks = List<Task<(List<string>, List<string>)>>()
tasks.Add(Task.Run(func() -> (List<string>, List<string>) { return work() }))
let results = await Task.WhenAll(tasks.ToArray())
}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: unverified
// 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: unverified
// 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
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::AsyncLet_OrderIndependentUse topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
func combine() -> Result<int, string> {
async let a = loadAsync(1)
async let b = loadAsync(2)
let bv = b?
let av = a?
return ok(av + bv)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::AsyncLet_ResultInterpolatedAfterUnwrap topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
func combine() -> Result<int, string> {
async let a = loadAsync(2)
async let b = loadAsync(2)
let av = a?
let bv = b?
let total = av + bv
return ok(total)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::AsyncLet_SecondError_Propagates topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
func combine() -> Result<int, string> {
async let a = loadAsync(3)
async let b = loadAsync(0 - 9)
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_AsyncLetMix.cs::AsyncUserFn_FirstError_ShortCircuits topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
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: ILEmitterTests_AsyncLetMix.cs::AsyncUserFn_SingleAsyncLet topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
func combine() -> Result<int, string> {
async let a = loadAsync(7)
let av = a?
return ok(av)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::AsyncUserFn_ThreePending topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
func combine() -> Result<int, string> {
async let a = loadAsync(1)
async let b = loadAsync(2)
async let c = loadAsync(3)
let av = a?
let bv = b?
let cv = c?
return ok(av + bv + cv)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_AsyncLetMix.cs::AsyncUserFn_TwoPending_OkFold topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
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_AsyncLetMix.cs::SyncUserFn_AsyncLet_Compiles topic: async status: unverified
// 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
}Rejected at compile time: ES2130
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_Let_Capture_In_Function_Literal_Is_OK topic: async status: unverified
// verified behavior: reports diagnostic ES2130
namespace Test
task func go() {
let value = 7
let read = func() -> int { return value }
let v = read()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_TaskFunc.cs::TaskFunc_With_Parameters_Reports_NotYetSupported topic: async status: unverified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
task func with_arg(n: int) -> int { return n }