Created
February 1, 2017 12:11
-
-
Save dszakallas/70142c71334c32ceb86d7d9f5d3b3518 to your computer and use it in GitHub Desktop.
avoiding callback pyramids
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
#!/usr/bin/env node | |
'use strict' | |
var mysql = require('mysql') | |
var fs = require('fs') | |
var path = require('path') | |
function exitOnError (f) { | |
return function (err) { | |
if (err) { | |
console.error(err) | |
process.exit(1) | |
} | |
f && f.apply(this, arguments) | |
} | |
} | |
function main (args) { | |
var url = args[0] | |
console.log(url) | |
var connection = mysql.createConnection({ | |
host: process.env.MYSQL_HOST || 'localhost', // | |
user: process.env.MYSQL_USER || 'root', // these should be | |
password: process.env.MYSQL_PASSWORD || '', // default install settings | |
database: process.env.MYSQL_DATABASE || 'information_schema' | |
}) | |
connection.connect(exitOnError(onConnect)) | |
function onConnect () { | |
// setup test user for password test | |
var sql = fs.readFileSync(path.resolve(__dirname, 'create_user.sql'), { encoding: 'utf8' }) | |
connection.query(sql, exitOnError(onQuery)) | |
} | |
function onQuery () { | |
connection.end(exitOnError()) | |
} | |
} | |
if (require.main === module) { | |
main(process.argv.slice(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment