Created
November 27, 2018 20:59
-
-
Save k-vosswinkel/d252d5b26a3bf1ac11ced5d6595f84fc to your computer and use it in GitHub Desktop.
Promise handling a database call
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
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