Skip to content

Commit

Permalink
Update function-use-case.md
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitshinde-84 authored May 3, 2024
1 parent ccab6d6 commit b52dffb
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/_includes/core-nodes/function-use-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ In Node-RED, a function node allows you to write custom JavaScript code to proce

In Node-RED, a function should either return an object, which is a message object, or nothing at all. Returning other data types instead of an object will cause an error. By default, the function node returns the message object unchanged, passing the data as it is to further nodes.

Ideally, the function node should return the message object at the end of the code written in the function node. Adding it in the middle will not execute the entire code.
Ideally, the function node should have the message object returned at the end of the code written within it. Placing the return statement in the middle of the code may result in incomplete execution of the remaining code.

If you want to pass a message object in the middle of the JavaScript code written in the function node, you can use `node.send()` to pass a message to subsequent nodes and continue executing the rest of the code, as shown below:

Expand Down Expand Up @@ -96,4 +96,4 @@ In the example flow below, we have an inject node generating a random number and

[{"id":"6a1f8dd5e5037d24","type":"inject","z":"a2240ea952051e81","name":"send random number","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$random() * 100","payloadType":"jsonata","x":180,"y":180,"wires":[["c4ef3a83b54cd6eb"]]},{"id":"c4ef3a83b54cd6eb","type":"function","z":"a2240ea952051e81","name":"Check against dynamic ranges","func":"// Extract numeric value from the incoming message\nconst numericValue = msg.payload;\n\n// Define ranges for conditional routing\nconst range1 = { min: 0, max: 10 };\nconst range2 = { min: 11, max: 20 };\nconst range3 = { min: 21, max: 30 };\n\n// Check numeric value against ranges\nif (numericValue >= range1.min && numericValue <= range1.max) {\n // Value falls within range 1\n return [msg, null, null];\n} else if (numericValue >= range2.min && numericValue <= range2.max) {\n // Value falls within range 2\n return [null, msg, null];\n} else if (numericValue >= range3.min && numericValue <= range3.max) {\n // Value falls within range 3\n return [null, null, msg];\n} else {\n // Value falls outside all ranges\n node.warn(\"Value falls outside all defined ranges.\");\n return null;\n}\n","outputs":3,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":180,"wires":[["a3af86e82cd2267a"],["35233d68a07ad23a"],["5a99ebc4ee439757"]]},{"id":"a3af86e82cd2267a","type":"debug","z":"a2240ea952051e81","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":140,"wires":[]},{"id":"35233d68a07ad23a","type":"debug","z":"a2240ea952051e81","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":180,"wires":[]},{"id":"5a99ebc4ee439757","type":"debug","z":"a2240ea952051e81","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":220,"wires":[]},{"id":"b1e8ca2faa3faafc","type":"comment","z":"a2240ea952051e81","name":"The function checks the numeric value against dynamic ranges and sends them to outputs accordingly.","info":"","x":450,"y":80,"wires":[]}]

{% endrenderFlow %}
{% endrenderFlow %}

0 comments on commit b52dffb

Please sign in to comment.