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

type Result record {
    string|error a;
    string|error b;
};

function multiFetch(string urlA, string urlB) returns Result {
    worker WA returns string|error {
        return fetch(urlA);
    }
    worker WB returns string|error {
        return fetch(urlB);
    }

    // The `wait` action can be used to wait for multiple named workers.
    return wait {a: WA, b: WB};

}

public function main() returns error? {
    Result res =  multiFetch("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();
}

Multiple Wait

The wait action can be used to wait for multiple named workers. Works with futures also.

import ballerina/http;
import ballerina/io;
type Result record {
    string|error a;
    string|error b;
};
function multiFetch(string urlA, string urlB) returns Result {
    worker WA returns string|error {
        return fetch(urlA);
    }
    worker WB returns string|error {
        return fetch(urlB);
    }
    return wait {a: WA, b: WB};

The wait action can be used to wait for multiple named workers.

}
public function main() returns error? {
    Result res =  multiFetch("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 multiple_wait.bal
{"a":"{"lang":"ballerina"}","b":"{"greeting":"hello"}"}
import ballerina/http;
import ballerina/io;

type Result record {
    string|error a;
    string|error b;
};

function multiFetch(string urlA, string urlB) returns Result {
    worker WA returns string|error {
        return fetch(urlA);
    }
    worker WB returns string|error {
        return fetch(urlB);
    }

    // The `wait` action can be used to wait for multiple named workers.
    return wait {a: WA, b: WB};

}

public function main() returns error? {
    Result res =  multiFetch("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();
}

Multiple Wait

The wait action can be used to wait for multiple named workers. Works with futures also.

import ballerina/http;
import ballerina/io;
type Result record {
    string|error a;
    string|error b;
};
function multiFetch(string urlA, string urlB) returns Result {
    worker WA returns string|error {
        return fetch(urlA);
    }
    worker WB returns string|error {
        return fetch(urlB);
    }
    return wait {a: WA, b: WB};

The wait action can be used to wait for multiple named workers.

}
public function main() returns error? {
    Result res =  multiFetch("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 multiple_wait.bal
{"a":"{"lang":"ballerina"}","b":"{"greeting":"hello"}"}

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