Function
Execute JavaScript functions with enhanced capabilities.
Function nodes are JavaScript snippets that allow for quick and easy parsing, re-mapping, or otherwise modifying data using JavaScript. These functions now support asynchronous operations and module imports, providing more flexibility and power.
Functions are executed on top of QuickJS Engine Isolates that are built into the ZEN Engine.
Maximum execution time of a single function is limited to 5000ms.
Getting started
To start, create a simple decision with input, function node, and output through the graph edit mode. Switch back to content edit mode and click on the function node (Open).
By default, a function will return anything that is provided as an input.
Writing a sample function
Let's create a function that will calculate the sum of all numbers provided in the request. Our expected JSON for the request will be:
{
"numbers": [1, 2, 3, 4]
}
And our function will look something like this:
/** @type {Handler} **/
export const handler = async (input) => {
const sum = input.numbers.reduce((acc, curr) => acc + curr, 0);
return { sum };
};
Importing modules
You can now import supported modules at the top of your function. Here's an example:
import Big from 'big.js';
import dayjs from 'dayjs';
import http from 'http';
import zen from 'zen';
/** @type {Handler} **/
export const handler = async (input) => {
// Your function logic here
return input;
};
Supported modules
dayjs
: For date and time manipulation.big.js
: For arbitrary-precision decimal arithmetic.zod
: For parsing and validating inputs and types.http
: A native Rust module for making HTTP requests. It provides an API similar to axios (e.g.,http.get
,http.post
).zen
: A native Rust module for using ZEN Engine (Rules Engine) from functions.
Asynchronous functions
Functions now support asynchronous operations. You can use async/await syntax for handling asynchronous tasks:
import http from 'http';
/** @type {Handler} **/
export const handler = async (input) => {
const response = await http.get('https://api.example.com/data');
const data = response.data;
// Process data
return data;
};
Debugging via Simulator
To debug a function, you may use console.log
statements. It supports all primitive data types which can be represented in JSON format.
As an example:
/** @type {Handler} **/
export const handler = async (input) => {
console.log('Input received:', input);
// Your function logic here
return result;
};
Updated 3 days ago