Skip to content

Instantly share code, notes, and snippets.

@JustinDFuller
Created August 12, 2018 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinDFuller/a334841f01192fc33ad436c1d8db85a5 to your computer and use it in GitHub Desktop.
Save JustinDFuller/a334841f01192fc33ad436c1d8db85a5 to your computer and use it in GitHub Desktop.
Using promise-funnel with a database
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