# Await and asynch errors within node-red node js file

**URL:** <https://discourse.nodered.org/t/await-and-asynch-errors-within-node-red-node-js-file/65123>\
**Category:** Developing Nodes\
**Created:** [15 July 2022 06:58 UTC](https://discourse.nodered.org/t/await-and-asynch-errors-within-node-red-node-js-file/65123 "2022-07-15T06:58:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chappo\_2](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@chappo\_2](https://discourse.nodered.org/u/chappo_2)\
**Post date:** [15 July 2022 06:58 UTC](https://discourse.nodered.org/t/await-and-asynch-errors-within-node-red-node-js-file/65123/1 "2022-07-15T06:58:54Z")

</div>

Hi, I am attempting to expand the functionality of the node-red-serial-port node by allowing it to take in the PID, VID and serial number of a serial device and connect to the the corresponding serial port, as opposed to just entering the desired serial port.

To do this, I need to retrieve the list of serial devices connected to the device currently. To my knowledge this is done like this:

```auto
                        var ports_list = [];
                        await SerialPort.list().then(function(ports){
                          ports.forEach(function(port){
                           ports_list.push(port);
                          })
                        });

```

I have tested this in a function node and it appeared to work correctly. However, when I attempted to implement it into the node-red file I got this error:

```auto
15 Jul 08:55:30 - [info] Waiting for missing types to be registered:
15 Jul 08:55:30 - [info] - serial-port (provided by npm module node-red-node-serialport)
15 Jul 08:55:30 - [info] - serial in (provided by npm module node-red-node-serialport)
15 Jul 08:55:30 - [info] To install any of these missing modules, run:
15 Jul 08:55:30 - [info] npm install <module name>
15 Jul 08:55:30 - [info] in the directory:

```

Although I am sure that the nodes are present as I do not get this error when I remove the added code.

I believe that it is an issue with the **await** function, as I do not get this error if I remove the **await** , however the list of serial devices cannot be collected.

Is there something I am missing in regards to using asynchronous functions in node-red?

Thanks

---

<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:** [15 July 2022 08:23 UTC](https://discourse.nodered.org/t/await-and-asynch-errors-within-node-red-node-js-file/65123/2 "2022-07-15T08:23:37Z")

</div>

Hi @chappo_2

If there was an error whilst loading the node it would have been logged when Node-RED started. That would hopefully give a bit more of a clue as to what error you are hitting in the code.

Based on what you've shared, if the error only happens when you are using the `await` keyword, are you using it inside a function that is properly declared to be `async`?

---

<div class="post-metadata">

**Author:** ![chappo\_2](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@chappo\_2](https://discourse.nodered.org/u/chappo_2)\
**Post date:** [15 July 2022 08:35 UTC](https://discourse.nodered.org/t/await-and-asynch-errors-within-node-red-node-js-file/65123/3 "2022-07-15T08:35:00Z")

</div>

Yep. I missed the actual error:

`15 Jul 09:14:33 - [warn] [node-red-node-serialport/serialport] SyntaxError: await is only valid in async functions and the top level bodies of modules (line:343)`

And yes. I was using it inside a function that wasnt decleared as async

---

<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:** [13 September 2022 08:35 UTC](https://discourse.nodered.org/t/await-and-asynch-errors-within-node-red-node-js-file/65123/4 "2022-09-13T08:35:58Z")

</div>

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