0

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.

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.

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.

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.

The code that's definitly producing the data:

const GetDataset = async (queryStr, callback) => {
    const pool = await poolPromise;
    const result = await pool.request().query(queryStr);
    console.log(result);
}

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.

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.

const BotListUpdate = (data) => {botList = data}

const GetDataset = async (queryStr, callback) => {
        const pool = await poolPromise;
        const result = await pool.request().query(queryStr);

        callback(result.recordset);
}

setInterval(() => {
    GetDataset('SELECT * FROM [MachinesInfo];', BotListUpdate);
    
    console.log(botList);
}, 5000);

I tried to put the function directly in the call:

GetDataset('SELECT * FROM [MachinesInfo];', (data) => {botList = data});

I'm out of imagination at this point.

Thanks.

Resolved with promise as suggested:

const BotListUpdate = (data) => {botList = data}

const GetDataset = async (queryStr) => {
        const pool = await poolPromise;
        const result = await pool.request().query(queryStr);
        return result.recordset;    
}

setInterval(() => {
    GetDataset('SELECT * FROM [MachinesInfo];').then((recordset) => botList = recordset);
    console.log(botList);
}, 5000);
Johnny Prescott
  • 263
  • 6
  • 23
  • The thing is, the `console.log` inside the interval runs right after you call `GetDataset` -- not after `BotListUpdate` is called. Ideally you don't use callbacks with async/await, because async/await is exactly what makes this easier. – Andrew Li Aug 12 '20 at 16:08
  • 1
    What you should do is remove the callback and just use promises as they were meant to be used: change `callback(result.recordset)` in `GetDataset` to just `return result.recordset`. Then use like `GetDataset('…').then((recordset) => console.log(recordset))` – Andrew Li Aug 12 '20 at 16:10
  • If you want a great overview of why your problem is happening and the solutions explained (callbacks and promises), give the duplicate a full read. It is worth it. – Andrew Li Aug 12 '20 at 16:13
  • @AndrewLi I was able to get to this point with callback at some point (not in me above code though). What I didn't realise is that it was working. I was using VSCode to watch the botList and it was always saying "Not Available" except when I breakpoint on it in the setinterval. If I just output it in console, it's definitly there. Not sure why VSCode can't see it at all time. – Johnny Prescott Aug 12 '20 at 18:46

0 Answers0