Function
Execute custom JavaScript or TypeScript code in your workflows
The Function block allows you to write and execute custom JavaScript or TypeScript code directly within your workflow. This powerful feature enables you to implement complex logic, data transformations, and integration with external libraries.
Overview
The Function block brings the full power of JavaScript/TypeScript to your workflows, allowing for:
- Custom data transformation and manipulation
- Complex conditional logic
- Mathematical calculations and algorithms
- Integration with external libraries
- Creation of reusable utility functions
How It Works
The Function block:
- Takes your custom JavaScript/TypeScript code
- Executes it in a secure, isolated environment
- Processes any inputs provided from previous blocks
- Returns the result for use in subsequent blocks
Configuration Options
Code Editor
A full-featured code editor where you can write your JavaScript/TypeScript code. The editor supports:
- Syntax highlighting
- Code completion
- Error checking
- Multiple lines of code
Inputs
Your function can access inputs from previous blocks through an input
object. For example:
Safety and Limitations
For security and performance reasons, function execution has certain limitations:
- Execution Time: Functions have a maximum execution time to prevent infinite loops
- Memory Usage: Memory is limited to prevent excessive resource usage
- Network Access: Network calls are restricted to prevent unauthorized access
- Available APIs: Only a subset of browser APIs are available
Inputs and Outputs
Inputs
- Code: Your JavaScript/TypeScript code to execute
- Input Data: Values from previous blocks that can be accessed in your code
Outputs
- Result: The value returned by your function
- Standard Output: Any console output from your function
- Execution Time: The time taken to execute your function (in milliseconds)
Example Usage
Here's an example of a Function block that processes customer data and calculates a loyalty score:
Best Practices
- Keep functions focused: Write functions that do one thing well
- Handle errors gracefully: Use try/catch blocks to handle potential errors
- Document your code: Add comments to explain complex logic
- Test edge cases: Ensure your code handles unusual inputs correctly
- Optimize for performance: Be mindful of computational complexity for large datasets