承诺的Koa.js请求已挂起 [英] Koa.js request with promises is hanging

查看:90
本文介绍了承诺的Koa.js请求已挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我搞砸了Koa.js和生成器,只是将一个简单的站点放在一起进行演示.我正在使用带有 node-sqlite3 的sqlite和Q来实现承诺.这是我的数据库代码:

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;
}

所以基本上我只是Q.defer来保证"对数据库的调用.然后,在我的Koa路线中,我有这个:

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});
});

我遇到的问题是请求只是挂起,浏览器从未得到响应.真正奇怪的是,如果我在yield db.getLogs()之后放置console.log语句,则可以看到db的结果.视图在那里,一切似乎都应该起作用,但事实并非如此.任何帮助我将不胜感激!

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!

推荐答案

好的,经过很多挫折之后,事实证明,当我尝试使用Q或bluebird时,都遇到了这个问题.当我切换到本地Promises时,它就如鱼得水.我将不得不进一步研究以弄清楚到底发生了什么,但是我会把它留在这里,以防将来有人碰到这个问题.另外,如果有人好奇,我正在使用节点0.11.13和q版本:1.1.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

这篇关于承诺的Koa.js请求已挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆