# On Start called multiple times

**URL:** <https://discourse.nodered.org/t/on-start-called-multiple-times/59603>\
**Category:** General\
**Created:** [9 March 2022 15:59 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603 "2022-03-09T15:59:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![augjoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/augjoh/32/26464_2.png) [@augjoh](https://discourse.nodered.org/u/augjoh)\
**Post date:** [9 March 2022 15:59 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/1 "2022-03-09T15:59:33Z")

</div>

When using "On Start" code of a Function node in a subflow, the code is called multiple times. This is unexpected, at least by me. Is there any way to suppress/configure this?

Example output:

```auto
9 Mar 16:54:36 - [info] Started flows
9 Mar 16:54:36 - [info] [function:833ae1fa0bc8173e] On Start
9 Mar 16:54:36 - [info] [function:9bb3532e2df19fe8] On Start

```

Example flows.json:

```auto
[
    {
        "id": "719bc69b20978590",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "93c9faba1011712c",
        "type": "subflow",
        "name": "Subflow 1",
        "info": "",
        "in": [
            {
                "x": 60,
                "y": 40,
                "wires": [
                    {
                        "id": "15348321803f2de8"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 340,
                "y": 40,
                "wires": [
                    {
                        "id": "15348321803f2de8",
                        "port": 0
                    }
                ]
            }
        ]
    },
    {
        "id": "15348321803f2de8",
        "type": "function",
        "z": "93c9faba1011712c",
        "name": "",
        "func": "\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "// Code added here will be run once\n// whenever the node is started.\nnode.log('On Start');",
        "finalize": "",
        "libs": [],
        "x": 200,
        "y": 40,
        "wires": [
            []
        ]
    },
    {
        "id": "fa74d793acb7fb35",
        "type": "inject",
        "z": "719bc69b20978590",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 160,
        "y": 60,
        "wires": [
            [
                "9747212c8784f910"
            ]
        ]
    },
    {
        "id": "85f5417e36908743",
        "type": "debug",
        "z": "719bc69b20978590",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 370,
        "y": 240,
        "wires": []
    },
    {
        "id": "9747212c8784f910",
        "type": "subflow:93c9faba1011712c",
        "z": "719bc69b20978590",
        "name": "",
        "x": 240,
        "y": 120,
        "wires": [
            [
                "cbbc22d504807358"
            ]
        ]
    },
    {
        "id": "cbbc22d504807358",
        "type": "subflow:93c9faba1011712c",
        "z": "719bc69b20978590",
        "name": "",
        "x": 300,
        "y": 180,
        "wires": [
            [
                "85f5417e36908743"
            ]
        ]
    }
]

```

---

<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:** [9 March 2022 16:03 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/2 "2022-03-09T16:03:32Z")

</div>

> [@augjoh](#):
>
> When using "On Start" code of a Function node in a subflow, the code is called multiple times

Because every instance of a subflow is its own entities. When you have 2 subflow instances in your flows, you have 2x everything inside the subflow template.

If you find this unexpected, then you are either using subflows wrongly or you need to use LINK CALL node.

Looking at your code, it looks like you want to create "utility functions" (or subroutines) - thats what the Link Call is for.

---

<div class="post-metadata">

**Author:** ![augjoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/augjoh/32/26464_2.png) [@augjoh](https://discourse.nodered.org/u/augjoh)\
**Post date:** [9 March 2022 16:12 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/3 "2022-03-09T16:12:44Z")

</div>

If a "LINK CALL" is a utility function/subrouting, what is a subflow good for? What are the benefits there?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 March 2022 16:27 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/4 "2022-03-09T16:27:26Z")

</div>

The Subflow allows you to specify parameters for that particular instance.  
Also you can use flow context within it, where the context is different for each subflow instance.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 March 2022 16:40 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/5 "2022-03-09T16:40:43Z")

</div>

Also a subflow can have multiple outputs and Status.

---

<div class="post-metadata">

**Author:** ![augjoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/augjoh/32/26464_2.png) [@augjoh](https://discourse.nodered.org/u/augjoh)\
**Post date:** [10 March 2022 17:53 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/6 "2022-03-10T17:53:50Z")

</div>

Subflows do not have a timeout. Is it possible to increase the LINK CALL timeout to infinite?

---

<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:** [10 March 2022 18:41 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/7 "2022-03-10T18:41:13Z")

</div>

No, but if you are not interested in the timeout use a catch node (pointed at the link call) to catch the error.

The question for me is, why are you getting timeouts?

---

<div class="post-metadata">

**Author:** ![augjoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/augjoh/32/26464_2.png) [@augjoh](https://discourse.nodered.org/u/augjoh)\
**Post date:** [28 March 2022 05:54 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/8 "2022-03-28T05:54:16Z")

</div>

The subflows are to complex to sort out all possible reasons for a timeout.

- What is the maximum timeout value possible? Is it possible to disable the timeout?
- What is the solution for Node-RED 1.3.7? There is no Link-Call available, yet.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 March 2022 08:50 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/9 "2022-03-28T08:50:47Z")

</div>

> [@augjoh](#):
>
> What is the maximum timeout value possible? Is it possible to disable the timeout?

I expect it is possible to set it to many hours, have you tried? How big do you need to set it?

> [@augjoh](#):
>
> What is the solution for Node-RED 1.3.7?

Upgrade to current version.

---

<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:** [11 April 2022 08:51 UTC](https://discourse.nodered.org/t/on-start-called-multiple-times/59603/10 "2022-04-11T08:51:29Z")

</div>

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