0

I'm new to JS. I have the below snippet in some code I copied:

let pool;
const createPool = async () => {
  pool = await mysql.createPool({
        user: 'me',
        password: 'guess',
        });
};
createPool();

I'm trying to add a property to "pool", like this, right after the createPool() invocation,

pool.db = 'stuff';

but I get "pool undefined". I cannot (or maybe I can?) add that property in the {} definition because it's conditional on some other logic (which I can execute either before or after the declaration).

Can someone kindly educate --

  1. What is "pool"?
  2. What is this strange looking function (?) createPool?
  3. Why is pool undefined after the createPool() call? (Even the "let" should instantiate it, no?)
  4. How do I add the db property?

Thank you very much. -lostInJS

Yanay Lehavi
  • 166
  • 11
  • Being new to JS I would recommend reading up on [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). But this is happening because you are not waiting for your function to resolve. – Get Off My Lawn Apr 02 '20 at 22:21
  • Does this answer your question? [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) – SuperStormer Apr 02 '20 at 22:24
  • (1) a variable, that is undefined until (2) that strange looking asynchronous function executed asynchronously and assigned a value to pool, so (3) either wait for that function to be done or move that assignment into the function after you assigned pool. – Jonas Wilms Apr 02 '20 at 22:28
  • @superStormer appreciate that you flagged that duplicate :) – Jonas Wilms Apr 02 '20 at 22:29

0 Answers0