javascript - 如何让程序等待一个函数在 Node 中完成运行?
问题描述
我有这两个功能:一个分析一些数据,另一个提供一些数据。在这里,分析数据的人翻译给定的文本并通过 IBM 的 Watson 上的 NLU 模型运行它。现在我只想将所有分析结果存储到 1 个单个数组中并将其记录在控制台中。
我的代码:
const analyzeTweet = function(tweet){
return new Promise((resolve,rejects)=>{
translator(tweet).then(translation => {
const options = {
text: translation.text,
features: {
entities: {model:auth.model},
relations:{model: auth.model}
},
};
nlu.analyze(options).then(responce => {
resolve(responce.result);
});
});
});
}
aggregate();
function aggregate(){
var analysis = []
incoming.forEach(tweet => {
result = analyzeTweet(tweet).then(result => {
analysis.push(result);
})
});
console.log(analysis);
}
在incoming
这里无关紧要
问题是当我运行脚本时,我得到这个输出:
[]
什么可能是错的或者我做错了什么。我需要深入学习任何概念吗?
解决方案
推荐阅读
- nginx - nginx 入口:在 nginx-controller 日志中忽略 yaml 定义中的冲突主机,但创建非工作情况
- python - 有没有一种安全的方式来存储网站的密码?
- c++ - DirectX 索引缓冲区 -> 索引缓冲区的开始
- addressbook - 如何使用 UserService 获取当前用户的完整详细信息,包括他们的默认地址?
- amazon-web-services - 如何重置 AWS codebuild 中的环境变量?
- c++ - 复合模如何在 C++ 中工作?
- c# - 如何使用代码在 Microsoft Teams 上启用可访问性
- django - Django - 每次从表中添加或删除索引都会对数据库和迁移产生问题吗?
- python - 服务器/客户端应用程序和 JSONDecodeError:未终止的字符串 python
- c - C 中的静态变量如何保存在内存中?