Swan Lake Beta2 is here.
import ballerina/http;
import ballerina/io;

// Fetch from A or B.
function altFetch(string urlA, string urlB) returns string|error {

    worker A returns string|error {
        return fetch(urlA);
    }

    worker B returns string|error {
        return fetch(urlB);
    }

    // The `wait` action can be used to wait for one of several workers.
    // This function will return as soon as the return value of either
    // `A` or `B` is available.
    return wait A | B;

}

public function main() returns error? {
    string res = 
        check altFetch("https://postman-echo.com/get?lang=ballerina",
                       "https://postman-echo.com/get?greeting=hello");
    io:println(res);
}

function fetch(string url) returns string|error {
    http:Client cl = check new (url);
    map<json> payload = check cl->get("");
    return payload["args"].toString();
}

Alternate Wait

The wait action can be used to wait for one of several workers.

import ballerina/http;
import ballerina/io;
function altFetch(string urlA, string urlB) returns string|error {

Fetch from A or B.

    worker A returns string|error {
        return fetch(urlA);
    }
    worker B returns string|error {
        return fetch(urlB);
    }
    return wait A | B;

The wait action can be used to wait for one of several workers. This function will return as soon as the return value of either A or B is available.

}
public function main() returns error? {
    string res = 
        check altFetch("https://postman-echo.com/get?lang=ballerina",
                       "https://postman-echo.com/get?greeting=hello");
    io:println(res);
}
function fetch(string url) returns string|error {
    http:Client cl = check new (url);
    map<json> payload = check cl->get("");
    return payload["args"].toString();
}
bal run alternate_wait.bal
{"lang":"ballerina"}
import ballerina/http;
import ballerina/io;

// Fetch from A or B.
function altFetch(string urlA, string urlB) returns string|error {

    worker A returns string|error {
        return fetch(urlA);
    }

    worker B returns string|error {
        return fetch(urlB);
    }

    // The `wait` action can be used to wait for one of several workers.
    // This function will return as soon as the return value of either
    // `A` or `B` is available.
    return wait A | B;

}

public function main() returns error? {
    string res = 
        check altFetch("https://postman-echo.com/get?lang=ballerina",
                       "https://postman-echo.com/get?greeting=hello");
    io:println(res);
}

function fetch(string url) returns string|error {
    http:Client cl = check new (url);
    map<json> payload = check cl->get("");
    return payload["args"].toString();
}

Alternate Wait

The wait action can be used to wait for one of several workers.

import ballerina/http;
import ballerina/io;
function altFetch(string urlA, string urlB) returns string|error {

Fetch from A or B.

    worker A returns string|error {
        return fetch(urlA);
    }
    worker B returns string|error {
        return fetch(urlB);
    }
    return wait A | B;

The wait action can be used to wait for one of several workers. This function will return as soon as the return value of either A or B is available.

}
public function main() returns error? {
    string res = 
        check altFetch("https://postman-echo.com/get?lang=ballerina",
                       "https://postman-echo.com/get?greeting=hello");
    io:println(res);
}
function fetch(string url) returns string|error {
    http:Client cl = check new (url);
    map<json> payload = check cl->get("");
    return payload["args"].toString();
}
bal run alternate_wait.bal
{"lang":"ballerina"}

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