FluxSharp Async/Await

Asynchronous programming with async functions and await expressions for non-blocking I/O operations.

What are async functions? Async functions allow your program to perform long-running operations (like network requests or file I/O) without blocking the entire program. The caller can continue doing other work while the async operation completes.

Async Functions

Basic Async Function

Declare an async function with the async keyword. The function can call await to wait for asynchronous operations to complete.

async public void FetchData() {
    string response = await GetURL("http://example.com");
    return;
}

Async Function with Return Type

Async functions can return values just like regular functions. Use the return type to specify what the function produces.

async public int ProcessData(string url) {
    string data = await FetchData(url);
    int result = data.length();
    return result;
}

Async Function with Parameters

Pass parameters to async functions to customize their behavior. Parameters work the same as in regular functions.

async public void Download(string url, int timeout) {
    byte[1024] buffer;
    buffer = await FetchBytes(url);
    return;
}

Await Expression

The await keyword pauses execution and waits for an async operation to complete before continuing.

Basic Await

Wait for an async function to complete:

async public void Main() {
    string response = await GetData();
    return;
}

Await in Assignment

Store the result of an async operation in a variable:

async public void Process() {
    string content = await FetchContent("url");
    int length = content.length();
    return;
}

Multiple Awaits

Execute several async operations sequentially. Each await waits for the previous one to complete:

async public void FetchAll() {
    string data1 = await GetData("url1");
    string data2 = await GetData("url2");
    string data3 = await GetData("url3");
    return;
}

Await in Conditions

Use await inside conditional blocks to perform async operations only when certain conditions are met:

async public void ConditionalAwait(string url) {
    if (url.length() > 0) {
        string response = await FetchURL(url);
        ProcessResponse(response);
    }
    return;
}

Async Statement

From grammar:

await_stmt = { await_keyword ~ ident ~ "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" ~ ";" }

Syntax:

await FunctionName();
await FunctionName(arg1, arg2);

Examples:

await ProcessData();
await FetchURL("http://example.com");
await Download(filename, timeout);

Declaring Async Functions

Simple Async

async public void FetchData() {
    await GetURL("url");
    return;
}

Async with Static

async public static void AsyncTask() {
    await SomeOperation();
    return;
}

Async Private

async private void InternalOperation() {
    await PrivateTask();
    return;
}

Using Async Functions

Awaiting Async Functions

async public void Main() {
    await FetchData();
    return;
}

Async in Loops

async public void ProcessAll(string[] urls) {
    for (int i = 0; i < 10; i++) {
        string response = await FetchURL(urls[i]);
        ProcessResponse(response);
    }
    return;
}

Async with Conditionals

async public void ConditionalFetch(bool should_fetch) {
    if (should_fetch) {
        string data = await GetData();
        ProcessData(data);
    }
    return;
}

Async Examples

Simple Fetch

async public void FetchFile(string filename) {
    byte[4096] content;
    content = await ReadFile(filename);
    return;
}

Download Multiple Files

async public void DownloadAll(string[] urls) {
    for (int i = 0; i < 10; i++) {
        byte[1024] data;
        data = await DownloadURL(urls[i]);
        SaveData(data, i);
    }
    return;
}

Async with Error Handling

async public void SafeFetch(string url) {
    string response = await GetURL(url);
    if (response.length() > 0) {
        ProcessResponse(response);
    }
    return;
}

Database Operations

async public int QueryDatabase(string query) {
    string result = await ExecuteQuery(query);
    int count = result.length();
    return count;
}

Async Rules

  1. Can only await inside async functions

``flux async public void Valid() { await Task(); // OK }

public void Invalid() { // await Task(); // ERROR - not in async } `

  1. Await must be on async operations

`flux async public void Example() { string data = await GetData(); // OK int x = 10; // OK (not async) } `

  1. Async functions return to caller

`flux async public void Task() { await Operation(); return; // Returns from async } ``

Keywords

async Keyword

Marks function as asynchronous:

async public void TaskName() {
    // Async body
}

await Keyword

Waits for async operation:

await AsyncFunction();
string result = await GetData();

Execution Model

  • Async functions can be called and awaited
  • Await pauses execution until operation completes
  • Multiple async operations can run concurrently
  • Cooperative multitasking (no preemption)

Example:

async public void Main() {
    // Async operations here
    await FetchData1();     // Runs first
    await FetchData2();     // Runs after first completes
    await FetchData3();     // Runs after second completes
}

Async Patterns

Sequential Operations

async public void Sequential() {
    string data1 = await Fetch1();
    string data2 = await Fetch2();
    string data3 = await Fetch3();
    
    // All complete sequentially
    return;
}

Independent Operations

async public void Independent() {
    // These could run concurrently if spawned separately
    await Task1();
    await Task2();
    await Task3();
    return;
}

Conditional Async

async public void Conditional(bool flag) {
    if (flag) {
        await AsyncOperation();
    } else {
        SyncOperation();
    }
    return;
}

End of Language Documentation