# Proper async await usage in function nodes

**URL:** <https://discourse.nodered.org/t/proper-async-await-usage-in-function-nodes/48113>\
**Category:** General\
**Created:** [7 July 2021 06:47 UTC](https://discourse.nodered.org/t/proper-async-await-usage-in-function-nodes/48113 "2021-07-07T06:47:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SewShulMeedeeUh](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@SewShulMeedeeUh](https://discourse.nodered.org/u/SewShulMeedeeUh)\
**Post date:** [7 July 2021 06:47 UTC](https://discourse.nodered.org/t/proper-async-await-usage-in-function-nodes/48113/1 "2021-07-07T06:47:56Z")

</div>

This forum [reply](https://discourse.nodered.org/t/telldus-api-async/19037/2) (to a similar question) seems to imply that a naked `await` should work within a function node...

> The call is asynchronous and returns a promise so you need to await for that to complete before you return

This SO [post](https://stackoverflow.com/a/51525281) (upper snippet) offers a pattern, which causes my flow to break when I `return msg;` (no downstream execution or output).

```auto
(async function() {
  let res = await pool.query('select 1 as somethingForExample;');
  
  msg.payload = res;
  return msg;
})()

```

(...based on the code snippet for querying Postgres [here](https://node-postgres.com/features/pooling/))

Checking node version in node-red I am using...

```auto
docker run --rm -it --entrypoint /bin/sh nodered/node-red:1.3.5-minimal
node -v

```

The version of node is...  
`v10.24.1`

Which according to [this SO](https://stackoverflow.com/a/59585976), the minimum required version for an assumed top level `async` (with which an `await` could be included) seems to be v13.

Can I `let x = await something();` in a function node? Do I need to wrap it in an `async`? How do I do so successfully given the pattern offered? Does [this announcement](https://nodered.org/blog/2019/08/16/going-async) by the node-red team that flows were transitioned to asynchronous (vs `async`, specifically) in v1.0 impact this issue?

---

<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:** [7 July 2021 06:54 UTC](https://discourse.nodered.org/t/proper-async-await-usage-in-function-nodes/48113/2 "2021-07-07T06:54:16Z")

</div>

That code doesn't work because the `return` statement returns from the wrapping function you've added. It doesn't return back to Node-RED. To send a message from inside a function, you'd use `node.send(msg)`.

But there is no need to use that pattern in the function node. You can use async at the top level inside the node.

```auto
// Assume you're getting pool from somewhere...

let res = await pool.query('select 1 as somethingForExample;');
  
msg.payload = res;
return msg;

```

---

<div class="post-metadata">

**Author:** ![SewShulMeedeeUh](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@SewShulMeedeeUh](https://discourse.nodered.org/u/SewShulMeedeeUh)\
**Post date:** [7 July 2021 07:01 UTC](https://discourse.nodered.org/t/proper-async-await-usage-in-function-nodes/48113/3 "2021-07-07T07:01:02Z")

</div>

> [@knolleary](#):
>
> You can use async at the top level inside the node.

Use it, as in, as your example literally illustrates (`async` isnt written and is assumed to be present)? Or, I would have to include it somewhere (presumably above the `// Assume...` block you provided)?

---

<div class="post-metadata">

**Author:** ![SewShulMeedeeUh](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@SewShulMeedeeUh](https://discourse.nodered.org/u/SewShulMeedeeUh)\
**Post date:** [7 July 2021 07:05 UTC](https://discourse.nodered.org/t/proper-async-await-usage-in-function-nodes/48113/4 "2021-07-07T07:05:06Z")

</div>

> [@knolleary](#):
>
> To send a message from inside a function, you'd use `node.send(msg)` .

Ah-ha! I now see the reference to `node.send` in the [docs](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously), thanks for that clarification!

Still hoping for feedback on intended placement/usage/assumptions for `async`

---

<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:** [7 July 2021 07:05 UTC](https://discourse.nodered.org/t/proper-async-await-usage-in-function-nodes/48113/5 "2021-07-07T07:05:58Z")

</div>

If you needed to use the word `async` anywhere, I would have said so.

I suggest you just try it and see.

---

<div class="post-metadata">

**Author:** ![SewShulMeedeeUh](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@SewShulMeedeeUh](https://discourse.nodered.org/u/SewShulMeedeeUh)\
**Post date:** [7 July 2021 07:11 UTC](https://discourse.nodered.org/t/proper-async-await-usage-in-function-nodes/48113/6 "2021-07-07T07:11:19Z")

</div>

> [@knolleary](#):
>
> I suggest you just try it and see.

I did initially and am troubleshooting some (other, apparently) buggy behavior. Thanks a million for helping rule that out as a cause. Greatly appreciated!

---

<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:** [21 July 2021 07:11 UTC](https://discourse.nodered.org/t/proper-async-await-usage-in-function-nodes/48113/7 "2021-07-21T07:11:55Z")

</div>

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