Can node functions return promises?

#1

Sorry for the noob question, I did poke around but couldn't find anything specific (might not have really known where to look exactly).

Anyway, I am writing my first node, and it looks like I basically create a module and export a function that takes an object as an argument.

I don't know what s expected from that function:

  1. What can/should it return?
  2. Can it return a promise?
  3. (and I guess related to #2) can the function be an async function?

Thanks in advance, just getting started here...

Thanks in advance! (and again, sorry for the noon question)

0 Likes

#2

Hi @ScottChapman

the basic structure of the node's javascript file is described here: https://nodered.org/docs/creating-nodes/first-node#lower-casejs

The Function you export will be called by the runtime when its time to load your node in. The single argument passed to that function is a handle into the Node-RED runtime - by convention, called RED. So within your function you would define you custom nodes along with their calls to RED.nodes.registerType to get them registered.

The Function can, if it wants, return a Promise.

As Node-RED still supports Node 4, then making your node function async would potentially mean it didn't work for some users.

However with the 0.20 release we'll be dropping Node 4 support so async functions would be okay.

1 Like

#3

Perfect thanks!

0 Likes