node.js异步编程解决方案之Promise
var dbBase = require('../db/db_base'); var school_info_db = require('../db/school_info_db'); function dbService(wxCorpid) { return new Promise(function(resolve, reject) { school_info_db.get_school_info(wxCorpid, function(err, val) { resolve(val); }); }) .then(function(schoolInfo) { return new Promise(function(resolve, reject) { resolve(dbBase.init(schoolInfo.dbConnString)); }) }) .catch(function(error) { // 处理前面Promise产生的错误 console.log('数据库连接池创建失败'); }); } async function optErrLog(wxCorpid) { var mysql = await dbService(wxCorpid); Promise.resolve(mysql) .then(function(mysql) { return new Promise(function(resolve, reject) { mysql.query('select * from bas_school;', function(err, rlt, feld) { resolve(rlt); }) }) }) .then(result => console.log(JSON.stringify(result))) } module.exports = { optErrLog: optErrLog }