Use function multiple times

One approach would be to use the global.set() with the memory module.

  1. store your function in a global variable:
global.set('sum', function(a, b) {
    return a + b;
}, 'memoryOnly');
  1. use your function:
msg.payload = global.get('sum', 'memoryOnly')(2, 3);

return msg;

Note: you need this setting in the settings.js:

    contextStorage: {
        default: {
	          module:"localfilesystem"
        },
        memoryOnly: {
            module: "memory"
        }
    },

Flow:

[{"id":"4ac1d184.c4df1","type":"inject","z":"66646ca8.e97654","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":2960,"wires":[["f3f7db1.61ac128"]]},{"id":"f3f7db1.61ac128","type":"function","z":"66646ca8.e97654","name":"","func":"global.set('sum', function(a, b) {\n    return a + b;\n}, 'memoryOnly');","outputs":1,"noerr":0,"x":340,"y":2960,"wires":[[]]},{"id":"1b0cf5f7.27cc2a","type":"function","z":"66646ca8.e97654","name":"","func":"msg.payload = global.get('sum', 'memoryOnly')(2, 3);\n\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":3020,"wires":[["da372639.45dec8"]]},{"id":"ddabba53.8d96e8","type":"inject","z":"66646ca8.e97654","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":3020,"wires":[["1b0cf5f7.27cc2a"]]},{"id":"da372639.45dec8","type":"debug","z":"66646ca8.e97654","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":3020,"wires":[]}]
2 Likes