From 946831210844120c564eddcfc07f7f1242ee9cb4 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Wed, 17 Apr 2024 09:42:07 +0530 Subject: [PATCH] Refactor the code and update description --- .../named_worker_with_on_fail_clause.bal | 25 +++++-------------- .../named_worker_with_on_fail_clause.md | 2 +- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/examples/named-worker-with-on-fail-clause/named_worker_with_on_fail_clause.bal b/examples/named-worker-with-on-fail-clause/named_worker_with_on_fail_clause.bal index b8d7ed0bbf..d1909258c3 100644 --- a/examples/named-worker-with-on-fail-clause/named_worker_with_on_fail_clause.bal +++ b/examples/named-worker-with-on-fail-clause/named_worker_with_on_fail_clause.bal @@ -1,31 +1,18 @@ 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"); diff --git a/examples/named-worker-with-on-fail-clause/named_worker_with_on_fail_clause.md b/examples/named-worker-with-on-fail-clause/named_worker_with_on_fail_clause.md index e89bcd25c4..d0ca877781 100644 --- a/examples/named-worker-with-on-fail-clause/named_worker_with_on_fail_clause.md +++ b/examples/named-worker-with-on-fail-clause/named_worker_with_on_fail_clause.md @@ -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 :::