Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate how to use IIFEs to write complex expressions. #2755

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions docs/code/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,18 @@ This expression:

### Example: Writing longer JavaScript

An expression contains one line of JavaScript. This means you cannot do things like variable assignments or multiple standalone operations.
You can do things like variable assignments or multiple statements in an expression, but you need to wrap your code using the syntax for an IIFE (Immediately Invoked Function Expression).

To understand the limitations of JavaScript in expressions, and start thinking about workarounds, look at the following two pieces of code. Both code examples use the Luxon date and time library to find the time between two dates in months, and encloses the code in handlebar brackets, like an expression.

However, the first example isn't a valid n8n expression:


```js
// This example is split over multiple lines for readability
// It's still invalid when formatted as a single line
{{
function example() {
let end = DateTime.fromISO('2017-03-13');
let start = DateTime.fromISO('2017-02-13');
let diffInMonths = end.diff(start, 'months');
return diffInMonths.toObject();
}
example();
}}
```


While the second example is valid:
The following code use the Luxon date and time library to find the time between two dates in months. We surround the code in both the handlebar brackets for an expression and the IIFE syntax.


```js
{{DateTime.fromISO('2017-03-13').diff(DateTime.fromISO('2017-02-13'), 'months').toObject()}}
{{(()=>{
let end = DateTime.fromISO('2017-03-13');
let start = DateTime.fromISO('2017-02-13');
let diffInMonths = end.diff(start, 'months');
return diffInMonths.toObject();
})()}}
```

## Common issues
Expand Down