types / mysql2

Typings for https://github.com/sidorares/node-mysql2
ISC License
41 stars 23 forks source link

Sample code from README never ends? #12

Closed fdcastel closed 7 years ago

fdcastel commented 7 years ago

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 7 years ago

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.