1

The await keyword can only be used inside an async function so I created a main() that is asynchronous and execute it at the global level. Everything runs correctly but then the program sits there in the event loop and never ends. I could add process.exit() but it seems heavy handed.

const mssql = require('mssql');

;(async function main() {
    console.log("Started");
    try {
        await mssql.connect(process.env.CONNECTION_STRING);
        const result = await mssql.query`SELECT CHECKSUM('a')`;
        console.dir(result);
        console.log("Success!");
    } catch (err) {
        console.log(err);
        console.log("Failed!");
    }
    console.log("Finished");
})();

I assume this has to do with the mssql module peculiarities more than anything.

Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112

1 Answers1

1

Is the connection closed automatically after a query or not?

If not I think the problem is that you need to close the connection and then it will end the process.

I hope this helped!

Laurent Dhont
  • 1,012
  • 1
  • 9
  • 22
  • 1
    Oh geez, I feel foolish now. I added `await mssql.close()` and it ended gracefully. This would be good to add to the example on the documentation: https://www.npmjs.com/package/mssql – Neil C. Obremski Nov 23 '19 at 19:26