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":[]}]

Flow Info

Created 7 years, 8 months ago
Updated 3 years, 11 months ago
Rating: 5 2

Owner

Actions

Rate:

Node Types

Core
  • comment (x1)
  • debug (x1)
  • function (x3)
  • http request (x1)
  • inject (x2)

Tags

  • callback
  • runflow
  • reuse
  • dry
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option