Sample code from README never ends?
fdcastel opened this issue · 1 comments
fdcastel commented
This code (from README, using non-promises version) hangs after displaying The solution is: 2
import {createConnection, QueryError, RowDataPacket} from 'mysql2';
const connection = createConnection(process.env['DB']);
connection.query('SELECT 1 + 1 AS solution', (err: QueryError, rows: RowDataPacket[]) => {
console.log('The solution is: ', rows[0]['solution']);
});
I had to add a
connection.end();
to the end of file to fix this. Then the program finishes normally.
Is this the expected behavior?
Using mysql2
version 1.2.0
. Node.js
version v6.10.3
.
felixfbecker commented
The example in the readme is copied directly from mysql2 and only adapted to use TypeScript, but I can provide an explanation nonetheless:
NodeJS only exits the process if there are no more events in the event loop and there are no more open streams, unless you unref
them. Typically Node is used for web applications that are supposed to run until terminated. So yes, this is expected.