0

I am a newbie in JavaScript, trying to implement code to connect to MySQL. I am using Promise along with Async/Await function. My goal is to get the return value of the Promise object.

However, I have read and tried every possible way but still failed to understand how the Asynchronous function works in JavaScript as the return value of the function getData() still return an object

Promise { < pending > }

I also tried to use then but it still returns the same result. I would really appreciate it if someone can show me what I have misunderstood or where I did wrong in this function. Thank you!

Here is my JS code:

function test_connection() {
  var connection = #MySQLConnectionObject;
  return new Promise((resolve, reject) => {
    connection.connect((error, result) => {
      if (error) {
        reject(error)
      } else {
        connection.end();
        resolve("Successfully Connected!");
      }
    });
  });
}

async function getData() {
  var result = await test_connection()
  return result;
}

var result = getData();
console.log(result);
Nam Dao
  • 77
  • 1
  • 2
  • 9
  • Inside an `async function` do something like `const foo = await getData()` so you await the value before use it. – 10110 Oct 14 '20 at 04:49
  • Yes, it *will* be a promise, so you must `await` it or put the code that needs the result into a `.then` callback. Doing both, as in `getData`, is nonsense. – deceze Oct 14 '20 at 04:51
  • @TheDaniel I had tried that and it still returns the same output Promise { }. – Nam Dao Oct 14 '20 at 04:54
  • @deceze I tried your way. The result is still the same. I still get the object Promise { } – Nam Dao Oct 14 '20 at 04:55
  • Can you try getting rid of the `.then(...)` in line 1 at `getData()`. You might be mixing `async/await` with `.then` syntax and I'm honestly not sure if that's valid. As `test_connection()` already returns something you should be able to do get away with simply `var result = await test_connection()` – 10110 Oct 14 '20 at 04:59
  • You tried *what* exactly? `getData` *will* always return a promise, by definition. You can’t unpromisify a promise. A promise will always resolve *sometime later*, so you need to wait for its resolution appropriately. – deceze Oct 14 '20 at 05:01
  • @deceze I just edited my code to make it a bit clearer. So what I am trying to do is to assign `var result = getData()`. I expected the function would return "Successfully Connected!" instead of object `Promise < pending >`. However, at the moment, it is still returning object Promise. – Nam Dao Oct 14 '20 at 05:09
  • @TheDaniel Yes, I fixed the code and tried that either. My variable is still returning Promise object :( – Nam Dao Oct 14 '20 at 05:10
  • 1
    It is *async execution*. The result will become available *sometime later*. You cannot have it *right now*. What you can have right now is a promise, and you can `.then` or `await` it. And it’s promises all the way down. `getData` does nothing, it just replaces one promise with another. Go read the two duplicates, you’re not the first one to need to wrap their head around this fundamental principle, and it’s explained at length there. – deceze Oct 14 '20 at 05:14
  • Do not skip through the duplicates looking for a quick fix solution. Take the time to read and comprehend. You’re missing a crucial core part of how Javascript and asynchronous programming works, and you need to take the time to really understand it in order to write any productive Javascript at all. – deceze Oct 14 '20 at 05:18
  • 1
    if you put `async` infront of an function then your "function" doesnt act like a function anymore. It acts like a promise. they explain it well https://javascript.info/async-await – bill.gates Oct 14 '20 at 05:28
  • 1
    Thank you all very much, especially @deceze and TheDaniel. It is true that I didn't read the duplicates carefully. I resolved my issue. :D – Nam Dao Oct 14 '20 at 08:12

0 Answers0