Skip to content

Instantly share code, notes, and snippets.

@jazzyjackson
Last active August 17, 2017 14:14
Show Gist options
  • Save jazzyjackson/bf494c79606b0bbd78a56922f30c288e to your computer and use it in GitHub Desktop.
Save jazzyjackson/bf494c79606b0bbd78a56922f30c288e to your computer and use it in GitHub Desktop.
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}))
})
})
}
@jazzyjackson
Copy link
Author

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'))

@jazzyjackson
Copy link
Author

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