Skip to content

Commit

Permalink
Update the alternative receiver code
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Apr 18, 2024
1 parent 8d9e931 commit 0ea111e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
51 changes: 31 additions & 20 deletions examples/alternate-receive/alternate_receive.bal
Original file line number Diff line number Diff line change
@@ -1,46 +1,57 @@
import ballerina/http;
import ballerina/io;
import ballerina/lang.runtime;

public function main() {
worker w1 {
2 -> w3;
fetch("https://postman-echo.com/get?worker=w1") -> w3;
}

worker w2 {
runtime:sleep(2);
3 -> w3;
runtime:sleep(3);
fetch("https://postman-echo.com/get?worker=w2") -> w3;
}

worker w3 returns int {
// The value of the variable `result` is set as soon as the values from either
worker w3 returns json|error? {
// The value of the variable `result` is set as soon as the value from either
// worker `w1` or `w2` is received.
int result = <- w1 | w2;
return result;
json|error result = <- w1 | w2;
return getJsonProperty(result);
}

worker w4 returns error? {
int value = 10;
if value == 10 {
return error("Error in worker 4");
}
value -> w6;
// invalid url
fetch("https://postman-echo.com/ge?worker=w4") -> w6;
}

worker w5 {
worker w5 returns error? {
runtime:sleep(2);
3 -> w6;
fetch("https://postman-echo.com/get?worker=w5") -> w6;
}

worker w6 returns int|error? {
worker w6 returns json|error? {
// Alternate receive action waits until a message that is not an error is received.
// Since `w4` returns an error, it waits further and sets the value that is received from `w5`.
int a = check <- w4 | w5;
return a;
// Since `w3` returns an error, it waits further and sets the value that is received from `w4`.
json|error result = <- w4 | w5;
return getJsonProperty(result);
}

int w3Result = wait w3;
json|error? w3Result = wait w3;
io:println(w3Result);

int|error? w6Result = wait w6;
json|error? w6Result = wait w6;
io:println(w6Result);
}

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

function getJsonProperty(json|error data) returns json|error? {
if data is error {
return data.message();
}
return data.'worker;
}
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 values from 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. Alternate receive action sets the first non-error value it encounters as the outcome.
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. Alternate receive action sets the first non-error value it encounters as the result.

::: code alternate_receive.bal :::

Expand Down
4 changes: 2 additions & 2 deletions examples/alternate-receive/alternate_receive.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
$ bal run alternate_receive.bal
2
3
w1
w5

0 comments on commit 0ea111e

Please sign in to comment.