# Backward-compatible done()

**URL:** https://discourse.nodered.org/t/backward-compatible-done/21536
**Category:** Developing Nodes
**Created:** [11 February 2020 09:04 UTC](https://discourse.nodered.org/t/backward-compatible-done/21536 "2020-02-11T09:04:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [11 February 2020 09:04 UTC](https://discourse.nodered.org/t/backward-compatible-done/21536/1 "2020-02-11T09:04:01Z")

</div>

I am in the process of making our nodes implement the new `on('input')` signature of Node-RED v1.0. I also noticed the error handling is now done via `done(err)` instead of `node.error(err, msg)`.

If there are multiple return points or error handling locations, the proposed way from the [docs](https://nodered.org/docs/creating-nodes/node-js#receiving-messages) and the [blog](https://nodered.org/blog/2019/09/20/node-done) can be quite verbose, because you'd always have to check if `done` is defined.

So I did the same as Nick (@knolleary ) did with `send` for the `done` callback.

It's working in both pre-1.0 and 1.0 for me. The question is, did I overlook some edge cases?  
If it's an approved solution, maybe this could be added to the docs as well. 🙂

Here's the verbose commented version:

```auto
done = done || function () {

    // at least one argument -> 'done' is used as error notification
    if (arguments.length > 0) {
        // call node.error()
        // use first arg as error object
        // 'msg' is taken from closure
        node.error.apply(node, [arguments[0], msg]);
    }

    // otherwise do nothing
};

```

And the modified example from the blog with the new `done` as one-liner:

```auto
this.on('input', function (msg, send, done) {
    // If this is pre-1.0, 'send' will be undefined, so fallback to node.send
    send = send || function () { node.send.apply(node, arguments) }
    done = done || function () { if (arguments.length > 0) { node.error.apply(node, [arguments[0], msg]); } };

    // do some work with msg
    someImaginaryLibrary(msg, (err, result) => {
        if (err) {
            // Report back the error
            done(err);
        } else {
            msg.payload = result;
            send(msg);
            done();
        }
    })
})

```

---

<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: [11 February 2020 09:43 UTC](https://discourse.nodered.org/t/backward-compatible-done/21536/2 "2020-02-11T09:43:54Z")

</div>

Here is how I do it at the moment:

```javascript
// If this is pre-1.0, 'send' will be undefined, so fallback to node.send
send = send || function() { node.send.apply(node,arguments) }
// If this is pre-1.0, 'done' will be undefined, so fallback to dummy function
done = done || function() { if (arguments.length>0) node.error.apply(node,arguments) }

```

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [11 February 2020 09:47 UTC](https://discourse.nodered.org/t/backward-compatible-done/21536/3 "2020-02-11T09:47:04Z")

</div>

Do you call it with both error and msg arguments as `done(err, msg)`? Otherwise catch nodes won't be triggered.

---

<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: [11 February 2020 09:56 UTC](https://discourse.nodered.org/t/backward-compatible-done/21536/4 "2020-02-11T09:56:48Z")

</div>

Yes, same as yourself.

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [11 February 2020 10:02 UTC](https://discourse.nodered.org/t/backward-compatible-done/21536/5 "2020-02-11T10:02:21Z")

</div>

No, there is a slight difference. 🙂

I call `done(err)`. The message is added from the closure in my wrapper function.

The reason was forward compatibility, if the actual `done` ever accepted more than one argument in the future. I tried to keep the API as-is.

---

<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: [11 February 2020 10:07 UTC](https://discourse.nodered.org/t/backward-compatible-done/21536/6 "2020-02-11T10:07:16Z")

</div>

In truth, I don't believe I've ever actually need to error out. All of my "errors" either recover or deliberately stop Node-RED because they wouldn't allow the node to continue. Those errors are generally only ever in the initial setup of the node.

Not to say that I shouldn't use it, just not used it so far.

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [11 February 2020 10:18 UTC](https://discourse.nodered.org/t/backward-compatible-done/21536/7 "2020-02-11T10:18:53Z")

</div>

I actually use `catch` nodes and proper error handling a lot, to get sort of a try/catch block.

At least it's important for our flows at work, because we have to return some meaningful response to the PLCs we're communicating with. Even if it's an error message. The operator needs to know what's going on. Not handling these would just result in a timeout, because the flow never finishes otherwise.

So all of our specialized custom nodes throw proper errors that can be handled accordingly.

---

<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 2020 10:18 UTC](https://discourse.nodered.org/t/backward-compatible-done/21536/8 "2020-04-11T10:18:56Z")

</div>

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