Saturday 22 September 2018

Gracefully handle a Promise error?

Hey,I am trying to learn how to use promises and what not. In my code, everything works just fine until I hit an error.The application hard exits on error. //dbManager.js const mysql = require("mysql"); const dbHost = "localhost"; const dbUser = "root"; const dbPassword = "password"; const dbName = "mydb"; const dbPool = mysql.createPool({ host: dbHost, user: dbUser, password: dbPassword, database: dbName }); module.exports.get = () => new Promise((resolve, reject) => { dbPool.getConnection((err, connection) => { if (err) { reject(err); return; }; resolve(connection); return connection; }); });  //app.js const db = require("./dbManager"); db.get() .then((conn) => { let sql = "Show Tables"; conn.query(sql, (err, results) => { if (err) console.log(err); console.log(results); conn.release(); }); }) .catch((e) => { //Please, stop exiting! console.log(e); });    How can I handle the error gracefully?Thank you for your time.

Submitted September 22, 2018 at 11:22PM by Ratatatah

No comments:

Post a Comment