Swan Lake Beta2 is here.
import ballerina/io;

function demo() returns future<int> {
    worker A returns int {
        return 42;
    }
    
    // Futures and workers are the same thing.
    // A reference to a named worker can be implicitly converted into a future.
    return A;

}

type FuncInt function () returns int;

function startInt(FuncInt f) returns future<int> {
    // `start` is sugar for calling a function with a named worker and returning
    // the named worker as a future.
    return start f();

}

public function main() returns error? {
    future<int> a = demo();
    int b = check wait a;
    io:println(b);

    future<int> c = startInt(() => 100);
    int d = check wait c;
    io:println(d);
}

Named Workers and Futures

Futures and workers are the same thing. A reference to a named worker can be implicitly converted into a future. start is sugar for calling a function with a named worker and returning the named worker as a future. Cancellation of futures only happens at yield points.

import ballerina/io;
function demo() returns future<int> {
    worker A returns int {
        return 42;
    }
    
    return A;

Futures and workers are the same thing. A reference to a named worker can be implicitly converted into a future.

}
type FuncInt function () returns int;
function startInt(FuncInt f) returns future<int> {
    return start f();

start is sugar for calling a function with a named worker and returning the named worker as a future.

}
public function main() returns error? {
    future<int> a = demo();
    int b = check wait a;
    io:println(b);
    future<int> c = startInt(() => 100);
    int d = check wait c;
    io:println(d);
}
bal run named_workers_and_futures.bal
42
100
import ballerina/io;

function demo() returns future<int> {
    worker A returns int {
        return 42;
    }
    
    // Futures and workers are the same thing.
    // A reference to a named worker can be implicitly converted into a future.
    return A;

}

type FuncInt function () returns int;

function startInt(FuncInt f) returns future<int> {
    // `start` is sugar for calling a function with a named worker and returning
    // the named worker as a future.
    return start f();

}

public function main() returns error? {
    future<int> a = demo();
    int b = check wait a;
    io:println(b);

    future<int> c = startInt(() => 100);
    int d = check wait c;
    io:println(d);
}

Named Workers and Futures

Futures and workers are the same thing. A reference to a named worker can be implicitly converted into a future. start is sugar for calling a function with a named worker and returning the named worker as a future. Cancellation of futures only happens at yield points.

import ballerina/io;
function demo() returns future<int> {
    worker A returns int {
        return 42;
    }
    
    return A;

Futures and workers are the same thing. A reference to a named worker can be implicitly converted into a future.

}
type FuncInt function () returns int;
function startInt(FuncInt f) returns future<int> {
    return start f();

start is sugar for calling a function with a named worker and returning the named worker as a future.

}
public function main() returns error? {
    future<int> a = demo();
    int b = check wait a;
    io:println(b);
    future<int> c = startInt(() => 100);
    int d = check wait c;
    io:println(d);
}
bal run named_workers_and_futures.bal
42
100

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