如何将一个承诺延迟到async.每一个完成
How to delay a promise until async.each completes?
如何将承诺延迟到异步操作完成?我正在使用异步和蓝鸟库。一旦我启动我的程序,done()函数返回一个错误,它是一个空的或几乎空的'masterlist'对象。为什么async不等待迭代器完成它的操作?
// bundler.js
var masterlist = {
"children": []
, "keywords": []
, "mentions": 0
, "name" : "newsfeed"
, "size" : 0
}
// initialize() returns a promise with the populated masterlist
exports.initialize = function() {
return new Promise(function(resolve, reject) {
// pullBreakingNews() returns a promise with the breaking news articles
nytimes.pullBreakingNews().then(function(abstracts) {
async.map(abstracts, iterator, done);
function iterator(item, callback) {
alchemyapi.entities('text', item, {}, function(response) {
// initialize each entity with masterlist
response.entities.forEach(function(entity) {
masterlist.children[masterlist.children.length] =
{
"abstract": item
, "children": []
, "name": entity.text
, "size": 0
};
masterlist.size += 1;
masterlist.keywords.push(entity.text);
});
callback(masterlist);
});
};
function done(err, results) {
if (err) {
console.log("ERROR: ", err);
} else {
resolve(results);
}
};
});
});
};
Firehose.js是调用initializer()的模块。我相信firehose会先运行,promise会在这个过程中被调用。Server.js => firehose.js => bundler.js => nytimes api
// firehose.js
// Compares entities to Twitter stream, counts every match
exports.aggregator = function(callback) {
bundler.initialize().then(function(masterlist) {
t.stream('statuses/filter', { track: masterlist.keywords }, function(stream) {
// read twitter firehose for incoming tweets.
stream.on('data', function(tweet) {
var tweetText = tweet.text.toLowerCase();
// sift through each tweet for presence of entities
masterlist.children.forEach(function(parentObject) {
// if the entity exists in the tweet, update counters
if (tweetText.indexOf(parentObject.name.toLowerCase()) !== -1) {
parentObject.size += 1;
masterlist.mentions += 1;
callback(masterlist);
}
});
});
});
});
};
非常感谢您的帮助
请不要把回调和承诺混在一起,只使用其中之一
// Do this somewhere else, it is only needed once
// it adds promise returning versions of all alchemy methods for you
Promise.promisifyAll(require('alchemy-api').prototype);
exports.initialize = function() {
return nytimes.pullBreakingNews().map(function(abstract) {
// Note that it is entitiesAsync that is a promise returning function
return alchemyapi.entitiesAsync('text', abstract, {}).then(function(response){
response.entities.forEach(function(entity) {
masterlist.children[masterlist.children.length] =
{
"abstract": abstract
, "children": []
, "name": entity.text
, "size": 0
};
masterlist.size += 1;
masterlist.keywords.push(entity.text);
});
});
}).return(masterlist);
};
并且你的initialize函数没有检查它是否已经初始化
迭代器的回调函数接受错误作为第一个参数。如果没有错误,您应该在那里传递一个假值(如null)而不是masterlist
。
function iterator(item, callback) {
alchemyapi.entities('text', item, {}, function(response) {
// initialize each entity with masterlist
response.entities.forEach(function(entity) {
masterlist.children[masterlist.children.length] =
{
"abstract": item
, "children": []
, "name": entity.text
, "size": 0
};
masterlist.size += 1;
masterlist.keywords.push(entity.text);
});
callback(null, masterlist);
});
};
相关文章:
- 为什么要包装每一个原型“;类“;JS中具有匿名函数的对象
- 一个接一个地淡出每一个li
- 使用jQuery'在数组中循环;s中的每一个都会在生成字符串时在开头产生一个未定义的值
- MongoDB从一组未知子文档中的每一个$slice
- 你如何使 Math.random 数组中的每一个第 9 个元素都是同一个元素?[JavaScript]
- JavaScript:哪个是更好的方法.为每一个或减少
- 如何记录node.js程序中的每一个“抛出”
- 有没有一种简单的方法来转换“;字体"style属性添加到其中的每一个'他的个人风格
- Javascript-Regex-用不同的字符串替换字符串中的每一个匹配项
- 我应该'使用strict'对于我编写的每一个javascript函数
- knockout.js事件,用于跟踪输入文本框内的每一个文本更改
- 如何简化和加快每一个
- 使用jQuery将一组元素中的每一个克隆到单独的目标元素中
- 一个Chrome扩展,显示随机图片从所选的本地文件夹为每一个新的选项卡
- 文本区域自动调整大小调整在每一个按键上的铬
- 查找每一个"
"在页面中并使用jQuery删除它 - 克隆每一个,附加到每一个
- 如何重新加载浏览器的每一个标签打开我们的网页
- 每一个都不像车把设想的那样工作
- 如何将一个承诺延迟到async.每一个完成