Inline Coding in CONNECT platform
As we discussed here, the core idea of CONNECT platform is that main logical flow of backend code is asynchronous. However, not all levels of the logic are necessarily asynchronous, so you would need to easily mix the asynchronous graphs of CONNECT with traditional synchronous code.
You can read on how to create CONNECT packages here. However, here we are going to cover how can you do inline code pieces on the platform.
Inline Codes Using Expressions
So this is how you would create an expression that gets a number and multiplies it by two:
Basically, when the object described in an expression or value is a function with zero arguments, the node will call the function and output its return value. In the context of this function, you also have access to all the inputs as they are named, as you can see in figure 2, that you can easily access
a in the function’s body.
In the function’s context, you also have access to these other objects:
consolewhich you can use for doing console logs and what not.
requirefunction, which you can use to require any module you might need.
errorfunction, which you can utilize to indicate an error has occurred. You can either pass it an error message (a string) or an
context: context is a shared object that is created for each request. All nodes that are invoked while processing that request can have access to it, read from it and write to it. For example, you can use
context.reqto access the express request object.
If you want to do a little bit of asynchronous logic in your code as well, then you can uses a function with one argument instead of zero:
For example, this code in an expression will run a node on path
/something on items of a list. The
done object is a function that you can call when you are done, and the data you pass to it would be the output of the expression. You can also call it without any arguments, and the output of the expression would be
Fun note: the
Check out the rest of the guides maybe? ;)