0

I am trying serialize connection function of mysql drive. But when I did run my function, nodejs make async calls and I can't retrieve the correct value of result. How I can run connection.connect in serialize? I can't use async/await to resolve this problem because the result of a function will turn a promise.

function a(){

 var result = "false";
 
 connection = dbConnection.getConnection();
 console.log("test1");
 
 connection.connect(function (err) {
  console.log("test2");
  result = "true";
 });

 console.log("end of function");
 return result;
}

console.log(a());

log:

test1

end of function

false

test2

Danilinn
  • 1
  • 1
  • 3
    To use `async/await` you would want your function to return a promise. – Max Baldwin Sep 14 '18 at 15:03
  • 3
    The ONLY thing you can do is learn how to code async. Either learn how to use callbacks properly or how to use promises or how to use async/await. Any other way you want to do it IS IMPOSSIBLE. – slebetman Sep 14 '18 at 15:03
  • 1
    There is no way around this problem without asynchronicity unfortunately. The good thing is asynchronicity isn't hard to implement at all. – Adam Sep 14 '18 at 15:06
  • Read [Up and Running with Asynchronous JavaScript](https://medium.com/@rcepeda1993/async-js-the-complete-guide-670b4cf906c6) – Rafael Sep 14 '18 at 15:10
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Roberts Sep 14 '18 at 15:16

1 Answers1

-1
function a() {
  return new Promise((resolve, reject) => {
    const connection = dbConnection.getConnection();

    connection.connect(function (err) {
      if (err) {
        reject();
      } else {
        resolve();
      }
    });
  });
}

async function isConnected() {
  let hasConnection = false;
  try {
    const connected = await a();
    hasConnection = true;
  } catch (err) {
    console.log('You are not connected');
  }

  return hasConnection;
}

isConnected();
Max Baldwin
  • 3,404
  • 3
  • 26
  • 40
  • This won't work. It is a syntax error to use `await` outside of `async` – slebetman Sep 14 '18 at 15:15
  • 1
    Why do you use both resolve/reject AND a boolean flag at the same time? This is redundant. And the `connected` constant is useless as it is never false. – Gherman Sep 14 '18 at 15:16
  • @slebetman right you gotta put the await inside of a function scope. Will fix – Max Baldwin Sep 14 '18 at 15:16
  • @Danilinn why can't you use promises? – Max Baldwin Sep 14 '18 at 15:20
  • in my application the result of my function can not be a promise, it has to be the pure result – Danilinn Sep 14 '18 at 15:21
  • At least don't reassign `connected`. It's const even – Gherman Sep 14 '18 at 15:21
  • @MaxBaldwin because I have an external function that will read my function a, and it can not open that result as promise. I am suing rcp protocol – Danilinn Sep 14 '18 at 15:24
  • @Danilinn, with `async/await` you don't have to handle promises in their normal `.then().catch()` manner. My point is your function that will be read by an external function has to return a promise so that you can make your asynchronous connection call synchronous. The external function does not need to be exposed to or handle that promise. – Max Baldwin Sep 14 '18 at 15:27
  • @MaxBaldwin the result of isConnected() is a promise, and i only read the status using .then().catch(), but in my application i cant do this. I need the pure status – Danilinn Sep 14 '18 at 15:31
  • @Danilinn that's not true. Look at my example above – Max Baldwin Sep 14 '18 at 17:05