# Expose configured output count to function node (I can PR)

**URL:** <https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848>\
**Category:** Feature Requests\
**Created:** [8 April 2021 09:03 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848 "2021-04-08T09:03:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kristian](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kristian/32/38743_2.png) [@kristian](https://discourse.nodered.org/u/kristian)\
**Post date:** [8 April 2021 09:03 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/1 "2021-04-08T09:03:08Z")

</div>

Right now if I wish to handle the configured output count "dynamically" inside function node, i'd also have to set it in javascript as the configured one is not exposed to the sandbox - so setting it twice.

My suggestion is to expose the output count as `node.outputs` inside the sandbox - a really easy addition to [10-function.js](https://github.com/node-red/node-red/blob/master/packages/node_modules/@node-red/nodes/core/function/10-function.js).

This should also be added to the [user guide about writing functions](https://nodered.org/docs/user-guide/writing-functions#multiple-outputs)

@knolleary If you're happy with this addition I can do PR for both.

* * *

My specific use case is function which debounces and counts inputs (counting consecutive button clicks) and i'd like to send output immediately and reset after output limit is reached.

For anyone intrested, here is my code:

```javascript
// How many milliseconds to wait for another click
const debounceMs = 1000;
// It would be nide to have node.outputs here :)
const outputs = 4;

// Read last state from node context
let counter = context.get('counter') || 1;
let timer = context.get('timer');

// We have existing timer which means this is consecutive click
if (timer) {
    clearTimeout(timer);
    counter++;
}

const sendMessages = () => {
    // Reset state
    context.set('counter');
    context.set('timer');
    // Send last message to corresponding output
    const messages = new Array(counter);
    messages[counter - 1] = msg;
    node.send(messages);
};

if (counter < outputs) {
    // We can still wait for another click
    timer = setTimeout(sendMessages, debounceMs)
    context.set('counter', counter);
    context.set('timer', timer);
} else {
    // Output limit reached, reset & send output immediately
    sendMessages();
}

```

Another use case could be some sort of load balancing between outputs.

This addition would allow write these functions generically only needing to configure the output count of the node itself.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 April 2021 09:46 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/2 "2021-04-08T09:46:59Z")

</div>

errr - outputs have to be wired to something to be useful - and then the changes need to be deployed to make those changes active... so I can't see how this could be useful ? If I started with one output (default) then the function somehow changed it to (say) 4 - the other 3 outputs wouldn't go anywhere... so ???

---

<div class="post-metadata">

**Author:** ![kristian](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kristian/32/38743_2.png) [@kristian](https://discourse.nodered.org/u/kristian)\
**Post date:** [8 April 2021 09:52 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/3 "2021-04-08T09:52:10Z")

</div>

Whoops, I didn't mean the node to change number of outputs, just get the configured value so I can write more generic functions which can handle any number of configured outputs without repeating the number in the code.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/83bafc346da7b8bdc6de4c1fec516319218a0a79.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 April 2021 09:53 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/4 "2021-04-08T09:53:45Z")

</div>

Ah - much clearer... ! personally I can't see a problem with adding that.

---

<div class="post-metadata">

**Author:** ![kristian](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kristian/32/38743_2.png) [@kristian](https://discourse.nodered.org/u/kristian)\
**Post date:** [8 April 2021 10:01 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/5 "2021-04-08T10:01:08Z")

</div>

Awesome 🙂 I'll get to 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:** [8 April 2021 10:27 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/6 "2021-04-08T10:27:43Z")

</div>

Better be quick... just about to record the 1.3 release video... 😉

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 April 2021 10:41 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/7 "2021-04-08T10:41:07Z")

</div>

Would `node.outputCount` or similar be a bit more accurate?

`node.outputs` kinda suggests to me this would be a collection/array/object of the outputs (and whatever properties they might have)?

---

<div class="post-metadata">

**Author:** ![kristian](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kristian/32/38743_2.png) [@kristian](https://discourse.nodered.org/u/kristian)\
**Post date:** [8 April 2021 10:50 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/8 "2021-04-08T10:50:27Z")

</div>

I do like the clarity of `node.outputCount`, but `node.outputs` would be consistent with how you write nodes.

@knolleary Any preference?

---

<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:** [8 April 2021 11:24 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/9 "2021-04-08T11:24:25Z")

</div>

`outputCount` is probably the clearest.

---

<div class="post-metadata">

**Author:** ![kristian](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kristian/32/38743_2.png) [@kristian](https://discourse.nodered.org/u/kristian)\
**Post date:** [8 April 2021 11:39 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/10 "2021-04-08T11:39:05Z")

</div>

`outputCount` wins then!

I have updated the PR as well: [Function node: add `node.outputCount` property to sandbox by kristianheljas · Pull Request #2918 · node-red/node-red · GitHub](https://github.com/node-red/node-red/pull/2918)

Any idea if this change will make it into 1.3?  
Would like to know for documenting 🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 April 2021 15:05 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/11 "2021-04-08T15:05:46Z")

</div>

looks like it's in ...

---

<div class="post-metadata">

**Author:** ![kristian](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kristian/32/38743_2.png) [@kristian](https://discourse.nodered.org/u/kristian)\
**Post date:** [8 April 2021 16:02 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/12 "2021-04-08T16:02:53Z")

</div>

Awesome,

Following up with docs as well:

> <https://github.com/node-red/node-red.github.io/pull/214>

Let me know if I missed something! 🙂

---

<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:** [7 June 2021 16:03 UTC](https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/13 "2021-06-07T16:03:37Z")

</div>

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