Return
In this section, we will guide you on how to use the Return block.
Use Cases
The return statement is a crucial keyword in JavaScript functions, and it serves two main purposes:
- Returning a value: Sends the result of a function's computation back to the caller.
- Terminating a function: As soon as a return statement is executed, the function stops immediately, and the return value is sent out.
Here, we will focus only on the second use case since the first one is not applicable in Tapicker.
Here’s a function that performs a division operation:
function divide(a, b) {
if (b === 0) {
return 'Divisor cannot be zero'
}
const result = a / b
console.log(result)
return result
}
If the value of b
is 0
, the function returns an error message, and the following code is skipped. This behavior is somewhat similar to the continue statement in loops.
Now, let’s see another example of returning from a loop:
const items = ['a', 'b', 'c']
for (let [index, value] of Object.entries(items)) {
if (value === 'b') {
return
}
console.log(index, value)
}
Yes, in this case, return can be used in place of break.
Moreover, it can return from nested loops:
const items = ['a', 'b', 'c']
for (let [outerIndex, outerValue] of Object.entries(items)) {
for (let [innerIndex, innerValue] of Object.entries(items)) {
if (outerValue === 'a' && innerValue === 'b') {
return
}
console.log(outerValue, innerValue)
}
}
Unlike break, return can exit multiple loops, whereas break only exits the innermost loop.
In Tapicker, Return behaves similarly to the examples above.
Basic Return
If you wish to stop the workflow, you can do so like in the following example. Typically, it is not used alone but rather with the Condition block, unless you are debugging.
As shown in the image, after executing the Set Variables block, the workflow encounters a Return, and the process stops. The Sleep block will not be executed.
Return Scope
Return is subject to scope limitations. In Tapicker, the scopes are as follows:
- Root
- Tab
- Window
When a return occurs, if neither Tab nor Window is encountered, the return continues to the Root.
Returning within a Tab
Here is an example of a return within a tab. Next, we will explain how it works so you can better understand it.
Note the annotations in the image. We’ve clearly marked the execution sequence: when the workflow reaches Step 4
, a return occurs, skipping the Sleep block. After the New Tab block is executed, the workflow continues to Step 5
with the Print Log block.
Returning within a Window
Returning within a window behaves the same way as within a tab, so we won't elaborate further here.