Skip to content

Instantly share code, notes, and snippets.

@k-vosswinkel
Created November 27, 2018 20:59
Show Gist options
  • Save k-vosswinkel/d252d5b26a3bf1ac11ced5d6595f84fc to your computer and use it in GitHub Desktop.
Save k-vosswinkel/d252d5b26a3bf1ac11ced5d6595f84fc to your computer and use it in GitHub Desktop.
Promise handling a database call
const queryADatabase = function(mysql, query) {
return new Promise((resolve, reject) => {
mysql.query(query, (err, rows) => {
if (err) reject(err);
else resolve(rows);
});
})
};
/* What we're doing here is creating a promise that resolves or rejects
based on the outcome of an SQL query. Breaking it down, our outermost function
takes a database instance and an SQL query. That function will return a promise.
That promise will reject if the SQL query throws and resolve (returning the
database info) if the query goes through successfully. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment