Skip to content

Commit

Permalink
Refactor the code and update description
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed May 7, 2024
1 parent 0398035 commit 6e9e6b9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
import ballerina/io;

function getIndex(int[] values, int value) returns int|error {
int? index = values.indexOf(value);
if index is () {
return error("value not found");
}

return index;
}

public function main() {
int[] values = [2, 3, 4, 5];
int value = 0;

worker w1 {
int index = check getIndex(values, value);
index -> w2;
index -> function;
} on fail {
// Handle the error thrown in the worker body.
-1 -> w2;
-1 -> function;
}

worker w2 returns int|error:NoMessage {
int|error:NoMessage result = <- w1 | w1;
return result;
}

int|error:NoMessage result = wait w2;
int|error:NoMessage result = <- w1 | w1;
io:println(result);
}

function getIndex(int[] values, int value) returns int|error =>
let int? index = values.indexOf(value) in index ?: error("value not found");
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Named worker with on fail clause

The `on fail` clause can be incorporated into a named worker, to handle any errors that occur within the worker's body.
The `on fail` clause can be used with a named worker, to handle any errors that occur within the worker's body.

::: code named_worker_with_on_fail_clause.bal :::

Expand Down

0 comments on commit 6e9e6b9

Please sign in to comment.