Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

请教个es7异步变同步的问题.懂的朋友麻烦不吝赐教!

$
0
0

function step() { var start = async () =>{ // 在这里使用起来就像同步代码那样直观

	var newsData=await db.query('SELECT id from topline WHERE newsNumber =55',function(err,rows){
		
		if(!err){
			return rows;
		}
		
	});
	
	console.log('开始了!');
	
	await db.query('SELECT id from topline WHERE newsNumber =55',function(err,rows){
		
		if(!err){console.log(rows)}
		
	});
	console.log('这里我想拿到数据库返回的值:'+newsData);
};

start()

}

//db.query的代码 var mysql = require(‘mysql’); var pool = mysql.createPool({ host: ‘localhost’, user: ‘root’, password: ‘’, database: ‘test’, port: ‘3306’ });

function query(sql, callback) { return new Promise((resolve, reject) => { pool.getConnection(function(err, connection) { // Use the connection connection.query(sql, function(err1, rows) { callback(err1,rows); resolve(); connection.release(); //释放链接 }); }); })

}

exports.query = query;

问题:我就是想拿到第一次打开数据库返回的值.这里拿不到了…他返回的是undefined


Viewing all articles
Browse latest Browse all 14821

Trending Articles