Skip to content

async — examples

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

Runs `gap4field` → 1

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


namespace Test
class Svc {
    var worker: Task
    var n: int
    init() { self.n = 0 }
    func work() -> Task { await Task.Delay(1) }
    func start() { self.worker = self.work() }
}
func gap4field() -> int {
    let s = Svc()
    s.start()
    return 1
}

Runs `gap4fwd` → 1

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


namespace Test
class Svc {
    var worker: Task
    var n: int
    init() { self.n = 0 }
    func start() { self.worker = self.run(2) }
    func run(k: int) -> Task { await Task.Delay(k) }
}
func gap4fwd() -> int {
    let s = Svc()
    s.start()
    s.worker.Wait()
    return 1
}

Runs `gap4` → 1

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


namespace Test
class Svc {
    var n: int
    init() { self.n = 0 }
    func work() -> Task { await Task.Delay(1) }
    func kick() {
        let running = self.work()
        running.Wait()
    }
}
func gap4() -> int {
    let s = Svc()
    s.kick()
    return 1
}

Runs `gap4ss` → 2

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


namespace Test
class Svc {
    var worker: Task
    var n: int
    init() { self.n = 0 }
    func run() -> Task { await Task.Delay(1) }
    func start() { self.worker = self.run() }
    func stop() -> Task { await self.worker }
}
func gap4ss() -> int {
    let s = Svc()
    s.start()
    let t = s.stop()
    t.Wait()
    return 2
}

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: TypeGrammarTests.cs::Chan_AnnotatedParamRoundTrip   topic: async   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func pump(ch: chan<int>, v: int) { ch.Send(v) }

func go() -> int {
    let ch = chan<int>(1)
    pump(ch, 13)
    var v = 0
    if ch.TryReceive(out v) { return v }
    return -1
}

Compiles

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

namespace Test
func go() -> int {
    let ch = chan<int>(1)
    var fired = 0
    select {
        .recv(v, ch) { fired = 1 }
        default      { fired = 99 }
    }
    return fired
}

Compiles

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

namespace Test
func go() -> int {
    let ch = chan<int>(1)
    ch.Send(42)
    var got = 0
    select {
        .recv(v, ch) { got = v }
        default      { got = -1 }
    }
    return got
}

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

struct 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

struct Config(name: string, url: string)
struct Item(title: string, source: string)
struct Status(name: string, itemCount: int)

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

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

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 }

Compiles

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

namespace Test
func go() {
    let ch = chan<int>(1)
    select {
        .poll(v, ch) { }
        default { }
    }
}