简体   繁体   中英

Why does my Node.js MS SQL script never end unless I CTRL+C out of it?

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM