Run code

The Run Code action node allows you to run Javascript code from your Apps towards third-party libraries. You may use Appfarm Data and Actions from the code.

Usage

You may add external Javascript libraries in Environment Config (Custom Header Tags). These libraries will be accessible from inside the Run Code.

Note that Run Code may run code asynchronously. In order to tell Appfarm when the code is finished running, you always need to finish the code with resolve() (or reject()).

If you want to run Actions from inside the Run Code, you must add the Action in the Actions list of the right-side property pane of the Run Code action node. Once added, you may find the Action in the Built in Function Params section of the Run Code function editor. Example: Having an Action Read Projects added to the Run Code, defined with Function Name readProjects, you may trigger it from inside the code by writing readProjects().

An example setup of Run code is showcased in our Showroom. You may view a demo as well as access the setup in Appfarm Create. If you do not have access, you may register here.

Remarks: Running Actions from Run Code

Executing an Action from Run Code is a javascript Promise behind the scenes. You need to tell (within the code) if the code should wait for the action execution to finish, or not, before resolving and exiting the Run Code block.

This one is best explained by some examples.

Not the best way (the "beginner approach")

// In this section, a call to the action myAction() is successfully initiated.
// But, the resolve() call executes immediately after, ending the Run Code action node.
// If myAction has any side-effects, the next steps in the code (or action nodes
// after Run Code), the business logic might not work as intended.
// Also: It myAction fails, nothing catches that error
myAction()
resolve()

The best way (the "professional approach")

// Let the action finish, then resolve. Also, handle errors.
myAction()
.then(resolve) // makes sure Run Code completes after myAction has completed
.catch(reject) // throw exception(error if myAction fails

You may also chain multiple actions in sequence

myAction()
.then(() => myOtherAction())
.then(resolve)
.catch(reject)

Last updated