# Setup and close of new function node: Discussion about use cases/extensions

**URL:** <https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570>\
**Category:** General\
**Created:** [5 July 2020 21:26 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570 "2020-07-05T21:26:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [5 July 2020 21:26 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/1 "2020-07-05T21:26:39Z")

</div>

Hi,

Starting with v1.1, the function node now has the setup and close tab. This initially got me very excited.

However, when thinking about possible use cases with the current functionality in more detail, I mostly can see the purpose in initializing context variables.

I came up with a few other ideas which would require quite a different philosophy of the node, so I would like to hear your opinions on that and maybe also what other use cases are you can think of with the functionality as it is today.

As far as I can see, the setup handler does not support all features of the main function body.  
For example, `node.status` cannot be used (workaround working today ` __node__.status`).  
But more importantly, I thought about using the setup handler to access an npm module, call its constructor and maybe establish some callbacks from the module, which then would send out a message if they are called. An example would be an npm module that connects to some service and has a callback when a message from the service is received. In order to do so, one would need access to the `send` and `done` function in the setup handler.  
It seems that such a functionality is against the philosophy of the function node in its current form. For example, the node would "wait" (in the async sense) until the code in setup handler has finished and then only goes on with processing messages.  
It is also not clear to me if storing an instance of the npm module in the context is a good idea in this case (to share it between the setup and the main function body)

The code in the close handler does, as far as I can see, not allow async function calls, which would also be required if the above mentioned functionality would be considered.

I personally would not be sure if I would use it much, since I could also just create a custom node, but maybe for one time things where you also don't need to create a ui for configuration, this might be an interesting idea, I think.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 July 2020 21:31 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/2 "2020-07-05T21:31:03Z")

</div>

> [@cinhcet](#):
>
> (workaround working today ` __node__.status` ).

Not read your whole post, but insta-reply to this. Do not use this. No discussion. Do not use.

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [5 July 2020 21:32 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/3 "2020-07-05T21:32:58Z")

</div>

> [@knolleary](#):
>
> Not read your whole post, but insta-reply to this. Do not use this. No discussion. Do not use.

no worries 🙂 I know that double underscore is the most clear way to state that you are not supposed to use it

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 July 2020 21:38 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/4 "2020-07-05T21:38:08Z")

</div>

Replying more fully, now I've read the whole thing.

This feature is one step in a series of changes coming down the roadmap. It isn't the end of it.

We have a design to review that will (optionally) make it easier to add npm dependencies to the function node: [https://github.com/node-red/designs/pull/31](https://github.com/node-red/designs/pull/31)

That then feeds into being able to export subflows themselves as npm modules. That would only work if the subflow was able to package it's dependencies without the user having to edit their settings file.

It terms of what node APIs are then exposed in the setup/close stages of the function, we've started small, exposing a bare minimum. It is easier to add then it is to take away. So yes, working through some more concrete uses of the new features will help identify what makes sense to expose.

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [5 July 2020 21:57 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/5 "2020-07-05T21:57:12Z")

</div>

(it was no critique in any way, I think it is totally sensible to start with a bare minimum, I just wanted to start a discussion for possible extensions)

I have read the design note and it is good idea to more easily add npm dependencies.

If I would summarize my requirements:

- expose `send`, `done` in the setup handler (when `done` is called, resolve the promise)
- expose `status`, `log`, `warn`, `error` (maybe also id, name etc.) both in setup and closing handler
- make closing handler optionally async by exposing `done`.

This would make the function node quite equivalent to a custom node (ignoring the ui).

I guess this would then allow to resolve the problem the user in this thread [Use a Wiegand RFID Reader](https://discourse.nodered.org/t/use-a-wiegand-rfid-reader/29565) wants to solve without writing a custom node.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 July 2020 12:57 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/6 "2020-07-07T12:57:29Z")

</div>

Some interesting ideas surfacing here. Nice.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 July 2020 13:09 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/7 "2020-07-07T13:09:11Z")

</div>

Apologies Nick, I added a comment to that request.

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [7 July 2020 23:17 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/8 "2020-07-07T23:17:22Z")

</div>

Hi,

I looked a little into this and implemented a "minimally invasive" version (i.e. with as little code changes as possible) of exposing `status, log, warn, error, debug, trace` as well as `send` in the setup handler.

Having `done` in the setup handler does not make too much sense I guess, since being an async function, one can achieve the behavior right now and since the setup code is executed in the constructor of the node, the `done` behavior of the runtime is not available anyway, is it?

I could file a PR, but maybe more discussion is needed first?

About the closing handler, if one would make it an async function, then exposing done would also not be needed.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 July 2020 23:24 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/9 "2020-07-07T23:24:28Z")

</div>

> [@cinhcet](#):
>
> maybe more discussion is needed

Yes, more discussion is needed. Raise the PR against the `dev` branch. At this point, that's where this discussion should happen regardless of what ends up going in.

But like I said, we aren't rushing to throw everything in, regardless of how "minimally invasive" the code change is. What matters is whether the functionality makes sense and doesn't cause unintended issues.

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [7 July 2020 23:41 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/10 "2020-07-07T23:41:25Z")

</div>

done [https://github.com/node-red/node-red/pull/2644](https://github.com/node-red/node-red/pull/2644)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 September 2020 23:41 UTC](https://discourse.nodered.org/t/setup-and-close-of-new-function-node-discussion-about-use-cases-extensions/29570/11 "2020-09-05T23:41:25Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
