Skip to content

Commit

Permalink
Update the multiple receive with url fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Aug 21, 2024
1 parent ab8b85b commit da4c772
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions examples/multiple-receive/multiple_receive.bal
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import ballerina/http;
import ballerina/io;
import ballerina/lang.runtime;

type Result record {
int a;
int b;
json|error a;
json|error b;
};

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

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

worker w3 returns json {
// The worker waits until both values are received.
Result result = <- {a: w1, b: w2};
return result.toJson();
}

json result = wait w3;
// The worker waits until both values are received.
Result result = <- {a: w1, b: w2};
io:println(result);
}

function fetch(string url) returns json|error {
http:Client cl = check new (url);
record {map<json> args;} payload = check cl->get("");
return payload.args.'worker;
}
2 changes: 1 addition & 1 deletion examples/multiple-receive/multiple_receive.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
$ bal run multiple_receive.bal
{"a":2,"b":3}
{"a":"w1","b":"w2"}

0 comments on commit da4c772

Please sign in to comment.