【发布时间】:2015-02-20 15:29:04
【问题描述】:
我正在使用 node.js 的 Q 库。我正在尝试打印一份报告,该报告将打印查询名称和结果。这就是我所拥有的。代码打印“In function undefined 。如何从“then”函数中访问 promise 对象的值?
var queries = ["2091 OR 2092 OR 2093",
"2061 OR 2062",
"2139 OR 2140 OR 2141"
];
var promises = new Array();
for (var i=0; i<queries.length; i++) {
promises[i]=performSearch(queries[i]);
promises[i].query = queries[i];
console.log("Outside function ", promises[i].query);
promises[i].then(function(data) {
console.log("In function ", this.query);
processSearchResults(data,this.query);
});
}
Q.allSettled(promises).then(function(results) {
endFunction();
});
【问题讨论】:
-
如果
performSearch返回一个promise,那么data也是一个promise。 -
@BenFortune:
data不是承诺 - 根据规范,它不得是承诺? -
@BenFortune 数据不是承诺。我已经在 promise 对象上设置了查询属性。我想从 then 函数中访问这个属性
标签: node.js asynchronous scope promise q