节点Js& Blubird用于并发mysql请求

时间:2016-04-15 09:30:53

标签: mysql node.js bluebird

我的目标是进行第一次查询以检索日期并通过socket.emit发送结果。然后对于每个日期,发出第二个请求,并在每个请求后通过socket.emit发送结果。

到现在为止我已经

var Promise    = require("bluebird");
var mysql      = require('mysql')
Promise.promisifyAll(require("mysql/lib/Connection").prototype);
Promise.promisifyAll(require("mysql/lib/Pool").prototype);

var pool = mysql.createPool({
connectionLimit : 10,
host : MYSQL_HOST,
database : MYSQL_DB,
user : MYSQL_USER,
password : MYSQL_PASS
})

var list
var pool_connection

pool.getConnectionAsync()
.then( (connection) => { 
    var query = "select ..."
    pool_connection = connection
    return connection.queryAsync(query)
 }) // step 1
.then( (rows) => { 
    list = []
    rows.forEach( (row) => { 
        list.push(row.added)
        socket.emit('...',{added:row.added})
    })
    return new Promise((resolve) => { resolve(true) })
}) // step 2
.then( (result) => {
    var promises = []
    list.forEach( (date) => {
        var query = "..."
        promises.push(pool_connection.queryAsync(""+query+""))
    })
    return Promise.all(promises)
}) // step 3
.then( (result) => { socket.emit('...',result })
.catch( (error) => { console.log(error) } )

它正常工作,但我可以在每次查询后发送(通过socket.emit)吗?

0 个答案:

没有答案