0

Ok, I give up on searching. Can anyone help me please with this whole JavaScript async behaviour? I am relativly new to JavaScript. What I want to achieve:
Open a connection to my sql database. Store that connection to a variable. Use this connection in my following code.
Problem: The opening of the connection is async, the code runs on and my connection variable is undefined.
So, how do I wait OUTSIDE of async functions that these functions are finished?
My Code:

'use strict';
const mssql = require('mssql');
let config = {...} // everything works inside here
let connection;
let connectFunction = async () => {
  return mssql.connect(config);
};
let result;
connectFunction().then(resolve => {
  connection = resolve;
  connection.request().query('SELECT * FROM users').then(result => {console.log(result);});
  // This would work, BUT I dont want to be stuck inside a thenified or async function
});
connection.request().query('SELECT * FROM users').then(result => {console.log(result);});
// This does not work, because the connectFunction would not be awaited.

I want to reuse the open connection later, so it would be stupid to work inside the connectFunction().then, because that would open the connection on every query.
How can I solve this the best way?

Marcel Grüger
  • 885
  • 1
  • 9
  • 25
  • You cannot wait without using an async function or using `then` callbacks. You're always stuck in either of them. – Bergi Jul 07 '19 at 16:51
  • *"how do I wait OUTSIDE of async functions"*: essentially you stay WITHIN (or use a `then` callback). The point is you need to embrace asynchrony and not expect things that will come some time later to magically be available ... *now*. – trincot Jul 07 '19 at 16:51
  • *"I dont want to be stuck inside a thenified or async function"*: why? Please explain what really makes that a problem in your case? – trincot Jul 07 '19 at 16:59
  • Opening the database is async and I want to reuse this connection. So I have to get the return value of this function outside of it. – Marcel Grüger Jul 07 '19 at 17:02
  • I could think of a way that would be a `while(true){}` and checking the connection state therin and leave only when I get an error or a working connection, but that seems 'dirty' to me. Isn't there a better way to wait for a Promise or async function to have reached the end? – Marcel Grüger Jul 07 '19 at 17:08
  • 1
    @MarcelGrüger `while(true)` doesn't even work, that just hangs your script. No, there is no other solution than waiting with an extra function, and you will never get the connection out of the asynchronous scope. – Bergi Jul 07 '19 at 17:27
  • 1
    @MarcelGrüger, [this](https://stackoverflow.com/a/28915678/3478010) might be of interest. – Roamer-1888 Jul 07 '19 at 17:31

1 Answers1

1

You can add multiple .thens to the same promise, so if you'd do:

  let connection = connectionFunction();

You can reuse that one connection everywhere:

  connection.then(db => {
   db.request().query("...");
  });

 connection.then(db => {
   db.request().query("...");
 });

So, how do I wait OUTSIDE of async functions that these functions are finished?

Soon there will be top level await, which allows you to write:

  let connection = await connectionFunction();

then connection would contain the connection itself and not a promise.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • That was exactly what I was searching for. And the info about the upcoming top level await is really interesting. Thank you very much. – Marcel Grüger Jul 08 '19 at 07:11