# Knowing when a node is done - new node API

**URL:** <https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737>\
**Category:** News\
**Created:** [20 September 2019 23:12 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737 "2019-09-20T23:12:49Z")\
**Posts on this page:** 13\
**Page:** 1

<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:** [20 September 2019 23:12 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/1 "2019-09-20T23:12:50Z")

</div>

If you have created your own nodes, there's a new callback function signature being introduced in Node-RED 1.0 that will allow the runtime to keep better track of when nodes are done with a message.

This is what drives the new 'Complete' node that is also being introduced. You can already see this in action with the current beta release, and there's no time like the present to start updating your nodes to the new API. The following blog post explains what's changing and how nodes can adopt the new API in an entirely backward compatible way.

The 'creating nodes' docs will be updated nearer the release.

> **[Knowing when a node is done](https://nodered.org/blog/2019/09/20/node-done)**
>
> There's a new callback function signature for handling messages in Node-RED 1.0. If you've created your own nodes, find out what's changing, what to update and how to stay backwards compatible.

---

<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:** [21 September 2019 00:59 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/2 "2019-09-21T00:59:52Z")

</div>

Nick, is there a way to target node-red versions for custom nodes?

For example, if I used a feature not present in pre V1 is there a way to limit installation of my node to avoid unnecessary negative feedback?

Edit ...  
Apologies if this is something I've missed in the documentation.

---

<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:** [24 September 2019 16:12 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/3 "2019-09-24T16:12:14Z")

</div>

Yes in the node-red section of package.json you can specify eg

```auto
    "node-red": {
        "version": ">=0.18.0",
        "nodes": {
            "serialport": "25-serial.js"
        }
    },
```

---

<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:** [24 September 2019 16:27 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/4 "2019-09-24T16:27:10Z")

</div>

Don't forget to target node.js if you need to as well. All of mine are now targetting node.js v8.16 as a minimum and Node-RED v0.18.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [24 September 2019 17:04 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/5 "2019-09-24T17:04:32Z")

</div>

> [@knolleary](#):
>
> adopt the new API in an entirely backward compatible way.

Great new feature.

It would nice not to have to test for the existence of `done` each time it is used. Would it be possible to do something like

```auto
send = send || node.send;
done = done || done0;

function done0(text,msg){
    if (arguments.length > 0) {
        return node.error(text,msg)
    } else {return}
}

```

Or have I missed a bunch of edge cases?

---

<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:** [25 September 2019 05:52 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/6 "2019-09-25T05:52:27Z")

</div>

This is similar to what I've done except that I've just included a dummy function for done unless I reach a situation where I need to call it with an error object which seems to be fairly rare in my nodes.

---

<div class="post-metadata">

**Author:** ![Hypnos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hypnos/32/7566_2.png) [@Hypnos](https://discourse.nodered.org/u/Hypnos)\
**Post date:** [26 September 2019 08:41 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/7 "2019-09-26T08:41:06Z")

</div>

if I setup:

```auto
        this.on('input', function (msg, send, done) { // eslint-disable-line complexity
            // If this is pre-1.0, 'send' will be undefined, so fallback to node.send
            send = send || node.send;
            // ...
            send(msg);
            done();
        }

```

In Node-red 1.0 it is working fine, in Node-Red 0.20.8 get the error:

```auto
26 Sep 08:34:08 - [info] [within-time-switch:39ac9572.49ebba] TypeError: this.metric is not a function
    at Node.send (C:\GFApps\nodejs\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\Node.js:216:10)
    at withinTimeSwitchNode.<anonymous> (U:\Development\github\node-red-contrib-sun-position\nodes\within-time-switch.js:299:25)
    at emitOne (events.js:116:13)
    at withinTimeSwitchNode.emit (events.js:211:7)
    at withinTimeSwitchNode.Node.receive (C:\GFApps\nodejs\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\Node.js:237:14)
    at InjectNode.Node.send (C:\GFApps\nodejs\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\Node.js:159:22)
    at InjectNode.<anonymous> (C:\GFApps\nodejs\node_modules\node-red\node_modules\@node-red\nodes\core\core\20-inject.js:78:26)
    at emitOne (events.js:116:13)
    at InjectNode.emit (events.js:211:7)
    at InjectNode.Node.receive (C:\GFApps\nodejs\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\Node.js:237:14)
26 Sep 08:34:08 - [error] [within-time-switch:39ac9572.49ebba] internal error within-time-switch:this.metric is not a function

```

---

<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:** [26 September 2019 08:45 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/8 "2019-09-26T08:45:35Z")

</div>

For starters, you have called `done()` which doesn't exist pre-v1. See the notes just above your post. So you need to fix that first and see what impact it has.

---

<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:** [26 September 2019 09:23 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/9 "2019-09-26T09:23:54Z")

</div>

@Hypnos thanks for reporting. Yes, you've found a problem with the recommended approach for migrating to the new api whilst keeping it backward compatible. I'm now curious as to why I didn't spot that in my testing.

The proper workaround is:

```auto
send = send || function() { node.send.apply(node,arguments) };

```

---

<div class="post-metadata">

**Author:** ![Hypnos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hypnos/32/7566_2.png) [@Hypnos](https://discourse.nodered.org/u/Hypnos)\
**Post date:** [26 September 2019 09:28 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/10 "2019-09-26T09:28:51Z")

</div>

Yes, you are right, in my real code I have `done = done || ...` as well. The error occurs on the `send(` line.

---

<div class="post-metadata">

**Author:** ![Hypnos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hypnos/32/7566_2.png) [@Hypnos](https://discourse.nodered.org/u/Hypnos)\
**Post date:** [26 September 2019 09:29 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/11 "2019-09-26T09:29:11Z")

</div>

Thank you!

---

<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:** [26 September 2019 09:30 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/12 "2019-09-26T09:30:16Z")

</div>

Ah, well that makes a decision for me - time for me to move some of my code to a new common library that will be pushed to npm independently so I only have to change it in one place.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 September 2019 22:36 UTC](https://discourse.nodered.org/t/knowing-when-a-node-is-done-new-node-api/15737/13 "2019-09-27T22:36:53Z")

</div>

> [@knolleary](#):
>
> The 'creating nodes' docs will be updated nearer the release.

I look forward to that, as this thread and the blog post has made difficult reading for me... especially the example given in the blog section **Backwards compatibility**.

I'm sure it say's more about my abilities, than the expression of the author.
