I have this JS code:
Download.prototype.insert = function(stocks, exchange, cb){
var self = this
, count = stocks.length
stocks.forEach(function(stock){
save(stock, done)
})
function save(stock, cb){
self.mysql.query("my query", [exchange.id, stock.Symbol], function(err, rows){
if (rows && rows.length > 0)
self.mysql.query("my query", [rows[0].id, stock.DateTime, stock.Close], cb)
else cb()
})
}
function done(){
count--
if (count === 0) return cb()
}
}
as you can see I pass a callback to the save()
function, I would like to know if i can separate the queries to avoid using of function(err, rows){ .... }
the problem is that function wait two parameters err
and rows
, so How can I also pass a custom third parameter (the cb()
function) ?
Thanks
I hope I understood your question right ^^
To wrap your callback parameter, you can create a function that returns an anonymous function (the actual callback). The cb parameter gets wrapped into the context of the anonymous function:
function queryDetails(cb){
return (
function(err, rows){
if(rows && rows.length < 0){
//mysql.query ...
}else{
cb()
}
})
};
mysql.query("query", ...., queryDetails(doneCallback));
Have fun!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.