diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index d5f14e8b2e..8ce2b5b96b 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -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 cl = check new (url); + map payload = check cl->get(""); + return payload["args"]; +} + +function getJsonProperty(json|error data) returns json|error? { + if data is error { + return data.message(); + } + return data.'worker; +} diff --git a/examples/alternate-receive/alternate_receive.md b/examples/alternate-receive/alternate_receive.md index acd1d7adba..314fd031d0 100644 --- a/examples/alternate-receive/alternate_receive.md +++ b/examples/alternate-receive/alternate_receive.md @@ -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 ::: diff --git a/examples/alternate-receive/alternate_receive.out b/examples/alternate-receive/alternate_receive.out index 47036fd588..69a8462f90 100644 --- a/examples/alternate-receive/alternate_receive.out +++ b/examples/alternate-receive/alternate_receive.out @@ -1,3 +1,3 @@ $ bal run alternate_receive.bal -2 -3 +w1 +w5