I am having trouble understanding co and how to leverage its functionality inside of another generator. Essentially I have a generator that needs to perform a SQL query, then do something complicated with each row before Yielding them out to the calling function (in the of loop).
The someGen code doesn't look right to me, and it isnt working as expected. So my question is twofold. First how can I leverage co-mysql inside of a generator yielding out objects created out of each row. Second I am struggling where to use co, and more importantly why.
No functioning attempt:
const co = require('co')
, mysql = require('co-mysql')
, MYSQL_RECORD_SET = 0
let pool = null
let getConnectionPool = function(){
if(!pool) pool = mysql.createPool(config.db);
return pool
}
let someGen = function*() {
co(function*() {
let connection = getConnectionPool()
let recordset = yield connection.query('SELECT ? as jobId', [ 99 ]);
if (recordset && recordset[MYSQL_RECORD_SET]) {
var jobs = _.map(recordset[MYSQL_RECORD_SET], function (row) {
return {id: row.jobId }
})
yield jobs
}
})()
}
for (let job of someGen()) {
console.log(job)
}