Created
November 2, 2020 02:37
-
-
Save danielomiya/0f8f37a824e0d47f39dd9e73bbdcfd84 to your computer and use it in GitHub Desktop.
Simple example of wrapping mysql functionalities on Promises and calling them with async/await interfaces.
This file contains hidden or 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
| // conn.js | |
| const mysql = require('mysql'); | |
| exports.openConnectionAsync = function openConnectionAsync() { | |
| const conn = mysql.createConnection({ | |
| // ... config | |
| }); | |
| return new Promise((resolve, reject) => { | |
| conn.connect(err => { | |
| if (err) return reject(err); | |
| return resolve(conn); | |
| }); | |
| }); | |
| }; | |
| // user-repository.js | |
| const { openConnectionAsync } = require('./conn'); | |
| exports.createUserAsync = async function createUserAsync(user) { | |
| const conn = await openConnectionAsync(); | |
| conn.query('insert into users set ?', user); | |
| conn.end(); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment