title: Returning variable through Async Callback not working
body: <p>I've been turning in circle searching for an answer to this for 2 days now so any help to guide me in the right direction would be appreciated.</p>
  <p>I am building a server side app that will query data from a Database un MsSql every x time and return the data to the various clients through sockets.</p>
  <p>The part I'm having an issue with is the first one. What I want to achieve is simple, I want to fetch the data from the DB and then store it in a variable accessible to other function so that my app can update the different clients with the right information everytime. I don't want to query the DB each and every single time as this wouldn't be very efficient or scalable.</p>
  <p>I know I need to use callback but I'm unsure why it doesn't work and I'm pretty I'm missing some knowledge about this.</p>
  <p>The code that's definitly producing the data:</p>
  <pre><code>const GetDataset = async (queryStr, callback) =&gt; {
      const pool = await poolPromise;
      const result = await pool.request().query(queryStr);
      console.log(result);
  }
  </code></pre>
  <p>I know that I needed to make those calls async with the mssql promise stuff. But then I need the content of result (The recordset), to be returned back to a function within setInterval so I can store it into my variable.</p>
  <p>Here's my non-working code that I lastly tried. I've tried so many combination I can't list them all. None of them were returning the result to a variable but just printing to console... which obviously works.</p>
  <pre><code>const BotListUpdate = (data) =&gt; {botList = data}
  
  const GetDataset = async (queryStr, callback) =&gt; {
          const pool = await poolPromise;
          const result = await pool.request().query(queryStr);
  
          callback(result.recordset);
  }
  
  setInterval(() =&gt; {
      GetDataset('SELECT * FROM [MachinesInfo];', BotListUpdate);
      
      console.log(botList);
  }, 5000);
  </code></pre>
  <p>I tried to put the function directly in the call:</p>
  <pre><code>GetDataset('SELECT * FROM [MachinesInfo];', (data) =&gt; {botList = data});
  </code></pre>
  <p>I'm out of imagination at this point.</p>
  <p>Thanks.</p>
  <p><strong>Resolved with promise as suggested:</strong></p>
  <pre><code>const BotListUpdate = (data) =&gt; {botList = data}
  
  const GetDataset = async (queryStr) =&gt; {
          const pool = await poolPromise;
          const result = await pool.request().query(queryStr);
          return result.recordset;    
  }
  
  setInterval(() =&gt; {
      GetDataset('SELECT * FROM [MachinesInfo];').then((recordset) =&gt; botList = recordset);
      console.log(botList);
  }, 5000);
  </code></pre>