Skip to content

Instantly share code, notes, and snippets.

@mooyoul
Created December 19, 2016 02:55
Show Gist options
  • Save mooyoul/cca40f678d06be500be8abcacb906d1c to your computer and use it in GitHub Desktop.
Save mooyoul/cca40f678d06be500be8abcacb906d1c to your computer and use it in GitHub Desktop.
// below line equal as
// const Promise = require('bluebird');
// const getConnection = Promise.promisify(pool.getConnection.bind(pool));
const getConnection = () => {
return new Promise((resolve, reject) => {
pool.getConnection((e, connection) => {
if (e) { return reject(e); }
resolve(connection);
});
});
}
const query = (conn, ...args) => {
return new Promise((resolve, reject) => {
conn.query(...args, (e, result) => {
if (e) { return reject(e); }
resolve(result);
});
});
};
co(function* () {
const mb_no = 1; // @note this is fake value
const connection = yield getConnection();
const memberData = yield query(connection, 'select * from member where mb_no = ?', [mb_no]);
conn.release();
if (!memberData) {
// @todo handle empty results
return console.error('empty results');
}
// do stuffs...
}).catch((e) => {
console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment