Skip to content

Instantly share code, notes, and snippets.

@wstam88
Last active May 27, 2021 04:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wstam88/8b14fb9099293049db728eadf7b4892c to your computer and use it in GitHub Desktop.
Save wstam88/8b14fb9099293049db728eadf7b4892c to your computer and use it in GitHub Desktop.
Run another flow with callback inside a function node

Description

This is an example how you could use/run another flow inside a node function. In the example I call another flow which makes a HTTP request to the 'Chuck Norris' joke api and afterwards runs the provided callback returning the payload from the request.

[{"id":"212831c5.2daa8e","type":"function","z":"df127fef.0cd23","name":"run callback","func":"msg.callback(msg.payload.value.joke.replace(/"/g, '\"'));","outputs":"0","noerr":0,"x":630,"y":580,"wires":[]},{"id":"127459ed.a814f6","type":"inject","z":"df127fef.0cd23","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":150,"y":580,"wires":[["bcc9902c.38276"]]},{"id":"bcc9902c.38276","type":"function","z":"df127fef.0cd23","name":"set getJoke","func":"var getJoke = function(firstname, lastname, callback) {\n firstname = firstname || 'Chuck';\n lastname = lastname || 'Norris';\n \n // http request\n msg.url = `http://api.icndb.com/jokes/random?firstName=${firstname}&lastName=${lastname}`;\n msg.method = 'GET';\n \n // assign callback to msg\n msg.callback = callback;\n \n // return copy of msg\n node.send(Object.assign({}, msg));\n};\n\n\n// make global\nglobal.set('getJoke', getJoke);","outputs":1,"noerr":0,"x":310,"y":580,"wires":[["fd8227d.b6269d8"]]},{"id":"874bac38.909f9","type":"comment","z":"df127fef.0cd23","name":"Flow callback example","info":"","x":160,"y":540,"wires":[]},{"id":"fd8227d.b6269d8","type":"http request","z":"df127fef.0cd23","name":"","method":"use","ret":"obj","url":"","tls":"","x":470,"y":580,"wires":[["212831c5.2daa8e"]]},{"id":"fc41d0dc.4d401","type":"inject","z":"df127fef.0cd23","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":160,"y":620,"wires":[["866162c0.13fd5"]]},{"id":"866162c0.13fd5","type":"function","z":"df127fef.0cd23","name":"run getJoke(firstname, lastname, callback)","func":"global.get('getJoke')('Wesley', 'Stam', function (joke) {\n msg.payload = joke;\n node.send(msg);\n});","outputs":1,"noerr":0,"x":400,"y":620,"wires":[["7f095419.73b7ec"]]},{"id":"7f095419.73b7ec","type":"debug","z":"df127fef.0cd23","name":"","active":true,"console":"false","complete":"false","x":650,"y":620,"wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment