Swan Lake Beta2 is here.
import ballerina/io;

public function main() {
    // `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() {
    future<int> fut = start foo();

start calls a function asynchronously.

    int|error x = wait fut;

wait for future<T> gives T|error.

    io:println(x);
}
function foo() returns int {
    return 10;
}
bal run asynchronous_function_calls.bal
10
import ballerina/io;

public function main() {
    // `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() {
    future<int> fut = start foo();

start calls a function asynchronously.

    int|error x = wait fut;

wait for future<T> gives T|error.

    io:println(x);
}
function foo() returns int {
    return 10;
}
bal run asynchronous_function_calls.bal
10

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand