问题
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!
回答1:
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
来源:https://stackoverflow.com/questions/27614882/koa-js-request-with-promises-is-hanging