【发布时间】:2015-06-22 03:54:45
【问题描述】:
关于使用 q 模块在 node.js 中使用延迟承诺,我有一些不明白的地方。这是一个说明我的问题的设置。
var Q = require('q');
var http = require('http');
var url = 'http://www.genenames.org/cgi-bin/download?' +
'col=gd_hgnc_id&' +
'col=gd_pub_eg_id&' +
'status=Approved&' +
'status_opt=2&' +
'where=&' +
'order_by=gd_pub_eg_id&' +
'format=text&' +
'limit=&' +
'submit=submit';
httpGet = function (url) {
var deferred = Q.defer();
var body = "";
http.get(url, function(res) {
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
// WE NEVER GET HERE ...
console.log(body);
deferred.resolve(body);
});
})
.on('error', function(err) {
console.log(err);
});
return deferred.promise;
};
var fetchData = function() {
var deferred = Q.defer();
httpGet(url)
.then(deferred.resolve())
.done();
return deferred.promise;
};
fetchData()
.then(function() {
console.log("I got here before data was downloaded!")
process.exit(0);
})
.catch(function(err) {
throw(err);
})
.done();
在 fetchData 中,“.then”函数在 httpGet() 完成下载数据之前被调用。我不明白为什么在调用 deferred.resolved 之前执行 .then。
如果我将 fetchData() 中的 deferred.resolve() 注释掉,那么一切都会按我的预期进行,尽管程序当然会挂起,因为最终的承诺永远不会实现。
有人能指出我在哪里出错了吗?
【问题讨论】:
-
你不应该在你的
fetchData函数中使用deferrred antipattern。为什么不只是return httpGet(url)?另外,.catch(function(err){throw err})什么都不做,删除它。 -
谢谢。好点。我对代码进行了一些编辑以进行清理,尽管我留下了您提到的反模式,因为这是我在最初的困惑中犯下的错误——我把它作为一个警示故事留给其他人。