# Async await inside a custom node

**URL:** <https://discourse.nodered.org/t/async-await-inside-a-custom-node/74273>\
**Category:** Developing Nodes\
**Tags:** async\
**Created:** [27 January 2023 18:38 UTC](https://discourse.nodered.org/t/async-await-inside-a-custom-node/74273 "2023-01-27T18:38:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuad\_rock](https://avatars.discourse-cdn.com/v4/letter/f/da6949/32.png) [@fuad\_rock](https://discourse.nodered.org/u/fuad_rock)\
**Post date:** [27 January 2023 18:38 UTC](https://discourse.nodered.org/t/async-await-inside-a-custom-node/74273/1 "2023-01-27T18:38:58Z")

</div>

i need to wait for my async function result "on input" event.  
Here is my code bellow

```auto
module.exports = function (RED) {
  const axios = require('axios');

  function MyNode(config) {

    RED.nodes.createNode(this, config);

  this.on('input', async function (msg) {

  let result = await getDirectory(url).then(
          res=>{
// get undefined
            console.log("Result 1: ", res);
          });
}
}

async function getDirectory(url){
 await axios.get(url)
      .then(response => {
// Get correct result
        console.log("Result 2: ", response);
        return response;
      })
      .catch(error => {

      });
}

```

I get a response from the getDirectory function but no response when I subscribe to it.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [27 January 2023 19:30 UTC](https://discourse.nodered.org/t/async-await-inside-a-custom-node/74273/2 "2023-01-27T19:30:08Z")

</div>

I _think_ there was a large mix of `await`, `then` when there shouldn't be.

try the below.

```javascript
const axios = require("axios");

module.exports = function (RED) {
  function MyNode(config) {
    RED.nodes.createNode(this, config);

    this.on("input", async function (msg) {
      try {
        let result = await getDirectory(msg.payload.url);
      } catch (Error) {
        // Handle
      }
    });

    function getDirectory(url) {
      return new Promise((resolve, reject) => {
        axios
          .get(url)
          .then((response) => {
            resolve(response);
          })
          .catch((error) => {
            reject(error);
          });
      });
    }
  }
};

```

Side Note: I updated the tag to async

---

<div class="post-metadata">

**Author:** ![tve](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tve/32/59475_2.png) [@tve](https://discourse.nodered.org/u/tve)\
**Post date:** [28 January 2023 02:09 UTC](https://discourse.nodered.org/t/async-await-inside-a-custom-node/74273/3 "2023-01-28T02:09:03Z")

</div>

I haven't used axios, but if you can write

```auto
axios.get(...).then(...)

```

it means that `get()` returns a Promise, which means you can await it.  
Thus the whole thing can be condensed into:

```auto
    this.on("input", async function (msg) {
      try {
        const result = await axios.get(msg.payload.url);
        // do something with result
      } catch (err) {
        // Handle
      }
    });

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [28 January 2023 07:44 UTC](https://discourse.nodered.org/t/async-await-inside-a-custom-node/74273/4 "2023-01-28T07:44:15Z")

</div>

98% compression ratio 🤣

---

<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 February 2023 07:44 UTC](https://discourse.nodered.org/t/async-await-inside-a-custom-node/74273/5 "2023-02-11T07:44:32Z")

</div>

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