javascript - javascript异步forEach循环
问题描述
我正在尝试对一组值(索引)运行 elasticsearch,但遇到了 javaScript 异步问题:
function(indices) {
let results = [];
indices.forEach(d => ESClient.search({
index: d.indexName,
body: {
query: {
match: {
first_name: 'fred'
}
}
}
})
.then(resp => results.push(resp))
)
}
索引中应该有三个元素,我应该如何从我的搜索中返回所有三个响应的结果?
解决方案
search(indices) {
return Promise.all(
indices.map((d) => {
return ESClient.search(yourElasticSearchPayload)
})
);
}
--------------------------------------------------------------------------
this.search(indices).then((results) => {
// do something with results
}).catch((reason) => {
// failure-reason of the first failed request
console.log(reason);
});
推荐阅读
- vue.js - Vue:等到父函数完成运行
- ubuntu - 使用 ImageMagick 从文本生成图像?
- haskell - 无法在堆栈项目中使用 nix 构建 hzk(haskell zookeeper)
- python - Django 不读取通过 AJAX 发布请求传递的 JSON 数据
- javascript - 散景触发 js_on_change 以首次显示图形
- github - 无法从 GitHub API 触发调度
- c - 函数返回链表中最旧的值
- java - 使用 Thymeleaf 在 HTML 上显示数据
- reactjs - React Graphql,从逗号分隔的字符串创建选择菜单
- vbscript - VBSCript CreateObject 变量使用不支持的自动化类型