Skip to content

Commit

Permalink
Swap function order and add more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Aug 23, 2024
1 parent 6a29e3d commit 5bb9e11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
44 changes: 24 additions & 20 deletions examples/alternate-receive/alternate_receive.bal
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,13 @@ import ballerina/io;
import ballerina/lang.runtime;

type Response record {
record {string 'worker;} args;
record {
string 'worker;
} args;
};

public function main() {
// Both arguments passed to the function `getFirstFetched` are valid URLs.
// Thus, the alternate receive action in worker `w3` sets the
// first value it receives from a worker as the result.
string? firstFetched = getFirstFetched("https://postman-echo.com/get?worker=w1",
"https://postman-echo.com/get?worker=w2");
io:println(firstFetched);

// The first argument passed to the `getFirstFetched` function is an invalid URL.
// The worker `w1` in `getFirstFetched` function returns an error.
// Thus, the alternate receive action in worker `w3` waits further
// and sets the value that is received from `w2` as the result.
firstFetched = getFirstFetched("https://postman-echo.com/ge?worker=w4",
"https://postman-echo.com/get?worker=w5");
io:println(firstFetched);
}

function getFirstFetched(string url1, string url2) returns string? {
// workers `w1` and `w2` fetches the content from the respective URLs.
worker w1 {
string|error result = fetch(url1);
result -> w3;
Expand All @@ -42,12 +28,30 @@ function getFirstFetched(string url1, string url2) returns string? {
return result is error ? () : result;
}

// The value returned from the worker `w3` is set to the variable `w3Result`.
string? w3Result = wait w3;
return w3Result;
}

function fetch(string url) returns string|error {
http:Client cl = check new (url);
Response {args: {'worker}} = check cl->get("");
return 'worker;
Response response = check cl->get("");
return response.args.'worker;
}

public function main() {
// Both arguments passed to the `getFirstFetched` function are valid URLs.
// Thus, the alternate receive action in worker `w3` sets the
// first value it receives from a worker as the result.
string? firstFetched = getFirstFetched("https://postman-echo.com/get?worker=w1",
"https://postman-echo.com/get?worker=w2");
io:println(firstFetched);

// The first argument passed to the `getFirstFetched` function is an invalid URL.
// The worker `w1` in `getFirstFetched` function returns an error.
// Thus, the alternate receive action in worker `w3` waits further
// and sets the value that is received from `w2` as the result.
firstFetched = getFirstFetched("https://postman-echo.com/ge?worker=w4",
"https://postman-echo.com/get?worker=w5");
io:println(firstFetched);
}
2 changes: 1 addition & 1 deletion examples/alternate-receive/alternate_receive.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Alternate receive

The alternate receive action can be used to receive one of multiple values corresponding to multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. The alternative receive action sets the first non-error value it encounters as the result and, in the case all channels return errors, it sets the last received error as the result.
The alternate receive action can be used to receive one of multiple values corresponding to multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. The alternative receive action sets the first non-error value it encounters as the result. If all the channels return errors, it sets the last received error as the result.

::: code alternate_receive.bal :::

Expand Down

0 comments on commit 5bb9e11

Please sign in to comment.