Description
This activity is used to run JavaScript code. It can be used for a wide variety of tasks such as maths operations, data processing, and others.
You can use the following libraries with this activity: 'lodash', 'moment', 'mustache'
.
A few notes about variables usage: the variables initialized within the Execute JS code activity are local which means they can not be accessed outside. However, the variables created using the 'Assign value to variable' activity are global and can be used and modified in the Execute JS code activity.
We also have support for these JavaScript libraries: lodash, momentjs, mustache. In order to call them, you need to use the reserved commands:
let newArr = _.cloneDeep(arr), //making a deep clone of an array through lodash
let today = moment(), //getting the current date through momentjs
let output = mustache.render("{{title}} spends {{calc}}", view) //using mustache
Note that we do not support operations with DOM-elements, browser APIs, or js libraries such as fs and similar. Arrow functions, classic functions, classes, and libraries such as Math are supported.
Editing code
The code is the main parameter of this activity. You can create and edite it in a separate built-in editor and store it in a .js file. This file is placed in the same folder where the .neek file is stored. If you delete this file, the data is lost.
To edit code, click the Edit Code button to open the editor.
Edit Code button
This is how the editor looks like.
Code editor in a new Studio Pro tab
You may also use other popular code editors to edit the code.
Note that you can see the list of used variables in the respective tab:
Example
Let us provide an example of how this activity can be used. We have this workflow that performs some calculations with two numbers, then saves the result in a variable and displays it in a notification window.
This workflow executes a JavaScript file
This is how the code looks like. As you can see, result variable is created outside of it using 'Assign value to variable' activity.
The code used in this example
Also note that the script file is saved locally.
Script file name and path
This is how we set the 'User notification' activity to display the output.
This displays a text message and the result variable value
And here is the result of this operation.
result variable value is displayed
Timeout
Optionally, you can use this parameter to set a time limit (in seconds) for the execution of the activity.
If any activity takes longer than the time you set to be executed, you can see a warning message on the console. After the timeout, you are able to stop the workflow by hitting the Ctrl+]
command.