javascript - 为什么我的 Promise.all().then() 在所述 Promise.all() 中的承诺之前执行
问题描述
我正在尝试在循环内的快速查询中的数组中添加一些值,并且我想在所述循环之外获取该数组。
app.get("/getMatchedProfile",function (req,res) {
con.query("SELECT\n" +
"\tidUser2\n" +
"FROM\n" +
"\t`match`\n" +
"WHERE\n" +
"\tidUser1 = ?", [req.query.id], function (error, results) {
if (error) throw error;
let taille = results.length;
let arrayPromise = [];//array of queries
let arrayMatch = [];//the array i want to retreive
console.log(taille);
for (let i = 0; i < taille; i++) {
arrayPromise.push(con.query("SELECT idUser1, idUser2 FROM `match` WHERE idUser1 = ?", [results[i].idUser2],
function (error, results) {
if (error) throw error;
for (let j = 0; j < results.length; j++) {
if (results[j].idUser2 == req.query.id) {
arrayMatch.push(results[j].idUser1);
console.log("1111 : "+arrayMatch)//should print first
}
}
}
)
)
}
Promise.all(arrayPromise).then(function() {
console.log("22222 : " + arrayMatch);//should print 2nd + not empty
}
);
});
});
首先打印“22222”,然后打印“11111”,应该是相反的位置
解决方案
推荐阅读
- qt - 如何在 ubuntu 上使用 clang 编译?
- javascript - 在 jQuery 中,鼠标事件触发错误的元素。我怎样才能解决这个问题?
- verilog - Verilog 按位或三元运算符失败
- javascript - 如何在 JAVASCRIPT 中缩写日期
- javascript - 如何使用从 SPO 返回列表项的 .filter 向查询添加第二个条件
- java - Docker-compose for Spring Boot 适配不同环境
- php - 获取两个单词之间的内容 - preg_match_all 多个模式
- spring - 从主机获取图像并在春季启动时将其显示在浏览器上
- reactjs - Loop through every field of object in react js
- javascript - 将字符串数组映射到我的数组类型 Angular