Last active
August 17, 2017 14:14
-
-
Save jazzyjackson/bf494c79606b0bbd78a56922f30c288e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mysql = require('mysql') | |
const config = ini.parse(fs.readFileSync('../credentials.ini', 'utf-8')) /* only happens once on server start */ | |
const db = mysql.createPool(config.labsdb) | |
tryMySQL(input){ | |
return new Promise((resolve, reject) => { | |
if(!input.mysql) return resolve(input) | |
db.getConnection((err, conn) => { | |
if(err) reject({dbError: err}) | |
conn.query(input.mysql) | |
.on('end', () => conn.release() || resolve(input)) | |
.on('error', err => reject({queryError: err})) | |
.on('fields', fields => this.send({fields})) | |
.on('result', row => this.send({row})) | |
}) | |
}) | |
} |
Very good for this.send to return the result as well so these try-promises can go downstream
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this strategy of rejecting with err and resolving with original input is a good way to do the promise chain recursive array.
If a 'try' is unnecessary in the case of 'post-chat' then it just moves to the next one
in the case of pre-chat, maybe you'll only try if some kind of err exists (Array.some(Object.keys(input).includes('err'))