node.js - nodejs process.exit() prevents function from executing properly -
i writing first nodejs script. below code have set test database connection.
when include process.exit()
@ end of script, nothing logged console - however, when remove line of code, function logs query results appropriately (output included also).
i wondering why process.exit()
@ end of code prevents function functioning , if using exit method wrong.
code:
/* * runs every minute on uptime agent * checks list of sites see if they're */ // strict mode (see http://stackoverflow.com/questions/8651415/what-is-strict-mode-and-how-is-it-used information) 'use strict'; // cuz im lazy string.prototype.lc = function() { return this.tolowercase(); } string.prototype.uc = function() { return this.touppercase(); } /** exceptions **/ var dbconnecterror = function(m) { this.name = 'dbconnecterror'; this.message = m; } var dbconfigerror = function(m) { this.name = 'dbconfigerror'; this.message = m; } /** databse **/ /* * change log * 08/07/2015 tyler j barnes * -- init dev */ var db = function() { // error messages this._errors = []; // connection state this._isconnected = false; // db configuration this._config = { host: '', user: '', password: '', database: '' }; // reference this._db = null; // connection obj ref this._con = null; // sql statement this._sqlstmt = ''; // prepared statement -- needs data binded this._isprepared = false; // data bind prepared stmts this._sqldata = null; // query result set this._result = null; /* * initialize * @param (object) : cofig prop , values * @return void */ this.ini = function(config) { // make sure config passed if(!config || (typeof config).lc() != 'object') { throw new dbconnecterror('invalid db configuration'); } // check appropriate properties // if exist, store value for(var p in this._config) { if(!(p in config)) { this._errors.push('missing database config: '+p+'\n'); } else { this._config[p] = config[p]; } } // throw errors before continue if(this._errors.length > 0) { var tmp = ''; for(var = 0; < this._errors.length; i++) { tmp+=this._errors[i]; } throw new dbconfigerror(tmp); } this._db = require('mysql'); }; // create connection -- returns thread id this.con = function() { this._con = this._db.createconnection(this._config); this._con.connect(); this._isconnected = true; }; // sets sql statement this.setsqlstmt = function(str, prepared, binddata) { this._sqlstmt = str; if(prepared) { this._isprepared = true; this._sqldata = binddata; } else { this._isprepared = false; this._sqldata = null; } }; // kills connection this.die = function() { if(this._isconnected) { this._con.end(); } }; } var c = { host: 'asdfasdf', user: 'asdfasdf', password: 'asdfasdf', database: 'asdfasdf' }; var d = new db(); d.ini(c); d.con(); d._con.query('select * agents', function(err,rows,fields) { if(err) console.log(err); console.log(rows); }); d._con.end(); // upsets me process.exit();
output when process.exit()
removed:
[{agentid:1, host: 'asdfasdfadf'....etc}]
the database query operation asynchronous. means concluded after program executes of main module, including exit
system call.
instead, should terminate process when results obtained database:
var d = new db(); d.ini(c); d.con(); d._con.query('select * agents', function(err,rows,fields) { if(err) console.log(err); console.log(rows); process.exit(); }); d._con.end();
this 1 of typical misunderstandings of how javascript's asynchronous function calls work. advise read on subject. these 2 questions may have useful content:
Comments
Post a Comment