# Calling async function in a custom node

**URL:** https://discourse.nodered.org/t/calling-async-function-in-a-custom-node/52187
**Category:** Developing Nodes
**Created:** [14 October 2021 02:29 UTC](https://discourse.nodered.org/t/calling-async-function-in-a-custom-node/52187 "2021-10-14T02:29:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)
#### Post date: [14 October 2021 02:29 UTC](https://discourse.nodered.org/t/calling-async-function-in-a-custom-node/52187/1 "2021-10-14T02:29:46Z")

</div>

I know the async topic has been discussed in a few posts here.  
But I have yet to find a step-by-step tutorial on how to call async function in a custom node.

I've tried the following

- adding async keyword to my node function like this

```auto
module.exports = function(RED) {
    async function myNodeFunc(config) {
        ....
        let result = await myHelperFunction(msg, config);
        ...
    }

```

With or without the async keyword above, however, this gives me the error below

```auto
SyntaxError: await is only valid in async function

```

How to achieve what I intend to do, i.e., calling an async function in the node input handler?

Sorry for the noob question, but I can't seem to find a straightforward instruction in the doc.

---

<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: [14 October 2021 08:33 UTC](https://discourse.nodered.org/t/calling-async-function-in-a-custom-node/52187/2 "2021-10-14T08:33:50Z")

</div>

I think that the problem is with the way you are exporting - though I'm not sure as I've not done it the way you have. The only modules I have with async functions in them were either exported as [closures](https://github1s.com/TotallyInformation/node-drayton-wiser/blob/HEAD/src/index.js) or as a [singleton class](https://github.com/TotallyInformation/node-red-contrib-uibuilder/blob/vNext/nodes/libs/package-mgt.js#L365)

---

<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: [14 October 2021 09:04 UTC](https://discourse.nodered.org/t/calling-async-function-in-a-custom-node/52187/3 "2021-10-14T09:04:14Z")

</div>

Hi @kakyoism

If you want to use `await` inside the input handler, then you need to make sure you declare the input handler as an async function.

In the code you've shared, you've made the top level node constructor function `async` - which is not the same thing as making the input handler async.

```auto
module.exports = function(RED) {
   function myNodeFunc(config) {
        
        // Make the handler for the input event async
        this.on("input", async function(msg,send,done) {
            let result = await myHelperFunction(msg, config);
        });
    }

```

---

<div class="post-metadata">

### Author: ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)
#### Post date: [27 October 2021 11:20 UTC](https://discourse.nodered.org/t/calling-async-function-in-a-custom-node/52187/4 "2021-10-27T11:20:41Z")

</div>

Thank you! @knolleary

---

<div class="post-metadata">

### Author: ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)
#### Post date: [27 October 2021 11:23 UTC](https://discourse.nodered.org/t/calling-async-function-in-a-custom-node/52187/5 "2021-10-27T11:23:01Z")

</div>

@knolleary BTW, is there a way to turn on line numbers in the console error log after node-red started?  
Problems like this wouuld have been easier to track down that way.

I'll try adding ESLINT to VS Code though, hope that'd help.

---

<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: [10 November 2021 11:24 UTC](https://discourse.nodered.org/t/calling-async-function-in-a-custom-node/52187/6 "2021-11-10T11:24:00Z")

</div>

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