0

So I'm messing around with Koa.js and generators, just threw together a simple site for demo purposes. I'm using sqlite with node-sqlite3 and Q for promises. Here's my db code:

module.exports.getLogs = function(){
    var deferred = Q.defer();
    var results = [];
    db.serialize(function(){
        db.each("SELECT ipAddress, action, details, timestamp FROM logs", function(err, row) {
            results.push({
                ipAddress: row.ipAddress,
                action: row.action,
                details: row.action,
                timestamp: new Date(row.timestamp)
            });
        }, function(){
            deferred.resolve(results);
        });
    });

    return deferred.promise;
}

So basically I'm just Q.defer to "promisify" the call to the database. Then, in my koa route, I have this:

app.get('/logs', function *(){
    var logs = yield db.getLogs();
    yield this.render('logs', {logs: logs});
});

The issue I'm having is that the request is just hanging, the browser never gets a response. What's really strange is that if I put a console.log statement after the yield db.getLogs() I see the results from the db just fine. The view is there, everything seems like it should work, but it simply doesn't. Any help would me much appreciated!

BFree
  • 102,548
  • 21
  • 159
  • 201
  • I'm guessing you don't need the second `yield`. –  Dec 23 '14 at 06:13
  • @torazaburo Thanks for looking. Turned out to be a weird issue see my answer to the question. – BFree Dec 23 '14 at 06:28
  • Just out of curiosity, can you share how you are promisifying the db call with Q and with native promises? –  Dec 23 '14 at 07:29

1 Answers1

2

OK, after much frustration, it turns out that when I tried either Q or bluebird, I was having this issue. As soon as I switched to native Promises, it worked swimmingly. I'll have to dig in some more to figure out what the heck is going on, but I'll leave this here in case anyone runs into this in the future. Also, if anyone is curious, I was running with node 0.11.13 and q version: 1.1.2

BFree
  • 102,548
  • 21
  • 159
  • 201