import ballerina/io;

public function main() returns error? {
    // `start` calls a function asynchronously.
    future<int> fut = start foo();

    // `wait` for `future<T>` gives `T|error`.
    int|error x = wait fut;
    io:println(x);
}

function foo() returns int {
    return 10;
}

Asynchronous Function Calls

start calls a function asynchronously and the function runs on a separate logical thread (“strand”): cooperatively multitasked by default Result will be of type future<T> and future is a separate basic type. Waiting for the same future more than once gives an error. Use f.cancel() to terminate a future.

import ballerina/io;
public function main() returns error? {
    future<int> fut = start foo();

start calls a function asynchronously.

    int|error x = wait fut;
    io:println(x);
}

wait for future<T> gives T|error.

function foo() returns int {
    return 10;
}
bal run asynchronous_function_calls.bal
10