现在有个需求,node http server启动之前,需要到mysql查询一些数据,根据查询得到的数据,http决定每次返回的数据
伪代码
var price = -1; var userId = 'some user provided value’; var sql = 'SELECT max(price) FROM users WHERE id = ' + connection.escape(userId); connection.query(sql, function(err, results) { price = results.price; });
server = http.createServer(function (request, response) { handler(request); }).listen(8012);
主要想问一下,查询mysql和处理http请求之间是异步的吗? 如果是异步的,怎么保证在处理http请求之前,我查询mysql然后做一写操作呢?