Created
August 12, 2018 01:36
-
-
Save JustinDFuller/a334841f01192fc33ad436c1d8db85a5 to your computer and use it in GitHub Desktop.
Using promise-funnel with a database
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
import mysql from 'mysql' | |
import createFunnel from 'promise-funnel' | |
const funnel = createFunnel() | |
funnel.cork() | |
const connection = mysql.createConnection({ | |
/* options here */ | |
}) | |
connection.connect(function () { | |
/** | |
* At this point query will begin to be invoked immediately | |
* And any queries that were queued up while waiting will be | |
* invoked. | |
*/ | |
funnel.uncork() | |
}) | |
/** | |
* Immediately export and allow use of | |
* A "query" function without having to | |
* worry about it being invoked before the connection | |
*/ | |
export default funnel.wrap(function query (queryString) { | |
return new Promise((resolve, reject) => { | |
connection.query(queryString, function (error, results) { | |
if (error) return reject(error) | |
return resolve(results) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment