The Code Block allows you to run arbitrary JavaScript code during your flow’s execution. This can be useful for a variety of purposes, such as:
The inputs to the code block are accessible in the special variable inputs
. To access the input named foo
, it is accessible on inputs.foo
.
All inputs have the following structure:
The type
of each input corresponds to a Data Type. The value
is the actual value of the input.
For example, if you have an input named myNumber
that is a number, you can access it in the code block like this:
The code block must return
an object containing the output values. Each property of the output object must correspond to one of the configured output names in the editor.
Each of the values of the properties must be an object with the {type: DataType; value: any}
structure described above.
For example, if you have an output named myNumber
that is a number, you can return it like this:
.slice
to get a substringCreate a Code Block with the following code:
Create a Text block, give it the value Hello World
, and connect it to the Code Block’s input
port.
Run the flow. Note that the Code Block’s output is Hello
.
Create a Code Block with the following code:
Rename the existing input of the Code block to input1
, and add a 2nd input to input2
. Add a 2nd output named length
.
Create two Text blocks, give them the values Hello
and World
, and connect them to the Code Block’s input1
and input2
ports, respectively.
Run the flow. Note that the Code Block outputs HelloWorld
and 10
.
If any error happens during the execution of the Code Block, then the block will error.
If you are unsure of the type of value passed into the code block, for example when reusing it in multiple places, you can and should check the type
of the input before using it. For example:
If you throw an Error
in the code block, then it will error.
Q: How is the code block implemented?
A: The Code Block is implemented using a Function constructor with, as of right now, only one argument passed in to it - inputs
. This is the same as if you were to write the following code:
Q: Can I use async
/await
in the code block?
A: No. The code block is executed synchronously, and does not support async
/await
. As you cannot use external libraries in a Code block, there is no need for async
/await
support anyway. Use an External Call Block instead.
Q: Can I use external libraries in the code block?
A: No. The code block is executed in a sandboxed environment, and does not have access to external libraries. Use an External Call Block instead.
Q: Can I use console.log
in the code block?
A: No, the console
variable is not available in a code block.
Q: Can the code block function like an If Block?
A: Yes. If you return the special value { type: 'control-flow-excluded', value: undefined }
from the Code block, then the blocks after the Code block will be excluded from the flow’s execution. This is useful if you want to conditionally execute a portion of the flow.
Q: Is there any timeout? What if I create an infinite loop?
A: There is no timeout. If you create an infinite loop, then the flow will hang indefinitely.
The Code Block allows you to run arbitrary JavaScript code during your flow’s execution. This can be useful for a variety of purposes, such as:
The inputs to the code block are accessible in the special variable inputs
. To access the input named foo
, it is accessible on inputs.foo
.
All inputs have the following structure:
The type
of each input corresponds to a Data Type. The value
is the actual value of the input.
For example, if you have an input named myNumber
that is a number, you can access it in the code block like this:
The code block must return
an object containing the output values. Each property of the output object must correspond to one of the configured output names in the editor.
Each of the values of the properties must be an object with the {type: DataType; value: any}
structure described above.
For example, if you have an output named myNumber
that is a number, you can return it like this:
.slice
to get a substringCreate a Code Block with the following code:
Create a Text block, give it the value Hello World
, and connect it to the Code Block’s input
port.
Run the flow. Note that the Code Block’s output is Hello
.
Create a Code Block with the following code:
Rename the existing input of the Code block to input1
, and add a 2nd input to input2
. Add a 2nd output named length
.
Create two Text blocks, give them the values Hello
and World
, and connect them to the Code Block’s input1
and input2
ports, respectively.
Run the flow. Note that the Code Block outputs HelloWorld
and 10
.
If any error happens during the execution of the Code Block, then the block will error.
If you are unsure of the type of value passed into the code block, for example when reusing it in multiple places, you can and should check the type
of the input before using it. For example:
If you throw an Error
in the code block, then it will error.
Q: How is the code block implemented?
A: The Code Block is implemented using a Function constructor with, as of right now, only one argument passed in to it - inputs
. This is the same as if you were to write the following code:
Q: Can I use async
/await
in the code block?
A: No. The code block is executed synchronously, and does not support async
/await
. As you cannot use external libraries in a Code block, there is no need for async
/await
support anyway. Use an External Call Block instead.
Q: Can I use external libraries in the code block?
A: No. The code block is executed in a sandboxed environment, and does not have access to external libraries. Use an External Call Block instead.
Q: Can I use console.log
in the code block?
A: No, the console
variable is not available in a code block.
Q: Can the code block function like an If Block?
A: Yes. If you return the special value { type: 'control-flow-excluded', value: undefined }
from the Code block, then the blocks after the Code block will be excluded from the flow’s execution. This is useful if you want to conditionally execute a portion of the flow.
Q: Is there any timeout? What if I create an infinite loop?
A: There is no timeout. If you create an infinite loop, then the flow will hang indefinitely.