# Async/Await use in NodesJS and Mysql query

**URL:** <https://forum.kirupa.com/t/async-await-use-in-nodesjs-and-mysql-query/643670>\
**Category:** web dev\
**Created:** [July 26, 2020, 4:51pm UTC](https://forum.kirupa.com/t/async-await-use-in-nodesjs-and-mysql-query/643670 "2020-07-26T16:51:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)\
**Post date:** [July 26, 2020, 4:51pm UTC](https://forum.kirupa.com/t/async-await-use-in-nodesjs-and-mysql-query/643670/1 "2020-07-26T16:51:54Z")

</div>

Hi guys, i am a bit confused when using Asyc/await, i am basically retrieving the data from the mysql DB and then use the information when constructing an object `user_details` that i will use later.  
I have defined the Asyc function and call right away and then implemented .then to resolve the promise but the promise doesnt seems to get resolved and when i console the `Invoice_No_Per_Trasaction` it shows as `Promise { <pending> }` not sure how i can overcome this issue .  
Any suggestions guys please. Thanks

```
let Invoice_No_Per_Trasaction = (async function Invoice_No() {

          connection.query("SELECT invoiceNo FROM users_basket WHERE users_user_id=?;", [user_id], function (err, results) {

            console.log("107", results); //RowDataPacket { invoiceNo: 1 },. ....

            return results;

          });

        })()

          .then((data) => {

            console.log("112x", data); // undefined

            return data;

          })

          .then((data) => {

            console.log("116", data); // undefined

            return data;

          });

        console.log("120x", Invoice_No_Per_Trasaction); //Promise { <pending> }

        let user_details = {

          user_FirstName: results[0].first_name, // name

          user_email: userIdentitiy, // emai@email.com

          Invoice_No: Invoice_No_Per_Trasaction, //Promise { <pending> }

        };
```

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 26, 2020, 7:38pm UTC](https://forum.kirupa.com/t/async-await-use-in-nodesjs-and-mysql-query/643670/2 "2020-07-26T19:38:36Z")

</div>

Async functions return promises that resolve to their return value. Your async function, Invoice\_No, doesn’t return anything so data in your first then() will be undefined.

Your call to connection.query() has a callback that returns a value, but that only returns from that callback. It doesn’t return from Invoice\_No. Invoice\_No would need its own return statement. And there’s no way for the callback to return from Invoice\_No; it can only return from itself.

If you want the callback value to get returned from Invoice\_No, you would need to wrap the call in a promise constructor. That promise can be returned from Invoice\_No and the connection.query callback can resolve it.

```
  async function Invoice_No() {
    return new Promise((resolve, reject) => {
      connection.query("SELECT invoiceNo FROM users_basket WHERE users_user_id=?;", [user_id], function (err, results) {
        if (err) {
          reject(err);
        } else {
          console.log("107", results); //RowDataPacket { invoiceNo: 1 },. ....
          resolve(results);
        }
      });
    })
  }

```

Note that at this point, `async` isn’t necessary for the function. Its only useful for making the function always return a promise (implicitly) and to allow you to use `await`. But Invoice\_No is explicitly returning a promise and not using `await` so it would still work fine not being `async`.

---

<div class="post-metadata">

**Author:** ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)\
**Post date:** [July 30, 2020, 8:32pm UTC](https://forum.kirupa.com/t/async-await-use-in-nodesjs-and-mysql-query/643670/3 "2020-07-30T20:32:26Z")

</div>

Thanks senocular.
