gpt4 book ai didi

asynchronous - 在 foreach 循环中,等待不起作用

转载 作者:行者123 更新时间:2023-12-03 22:21:09 25 4
gpt4 key购买 nike

在此代码下方下载controller.js

// Retrieve all downloads from the database.
exports.findAll = async (req, res) => {
downloadObj
.findAll(
{
include: [
{
model: composerObj,
required: false,
},
{
model: raagaObj,
required: false,
},
{
model: taalaObj,
required: false,
},
],
where: req.query,
raw:true,
nest: true,
})
.then((data) => {

data.forEach(async (element) => {
const artistsArray = [];
let whereConstraint = {};
JSON.parse(element.artists).forEach( async (ele) => {
artistsArray.push(ele.artistId);
});;
whereConstraint = {
id : {
[Op.in]: artistsArray
}
}
element.Active = "true11";
const artistData = artistService.customfindAll(whereConstraint);
element.artistData = artistData;
console.log("artistData",artistData);
});

console.log("data",data);
console.log("3");

res.status(200).send({
status:200,
message: "ok",
data:data
});

console.log("4");
})
.catch((err) => {
res.status(500).send({
status:500,
message:
err.message || "Some error occurred while retrieving Download.",
});
});
};
在此代码下方,artistServer.js 文件
exports.customfindAll = async (whereConstraint) => {
return new Promise(function(resolve, reject) {
artistObj
.findAll({
attributes:{
include: [
[fn('IF',literal('imagePath!=""'),(fn('CONCAT', artistImagePath, 'artist/', col('imagePath'))),artistImageDefaultPath), 'imageFullPath'],
],
},
where: whereConstraint,
order: [
['artistName', 'ASC'],
],
raw:true,
nest: true,
})
.then((data) => {
resolve(data);
});
});
}
我的问题是
const 艺术家数据 = 艺术家服务.customfindAll(whereConstraint);
不等数据。 .所以我得到了结果,下面有提到。实际上是artistData属性列,需要结果数据。
{
"status": 200,
"message": "ok",
"data": [
{
"id": 1,
"fileType": "1",
"customFileName": "test filename",
"artists": "[{\"artistId\":1},{\"artistId\":4},{\"artistId\":2}]",
"accompanyingArtists": "[{\"instrumentId\":1,\"accompanyingArtistId\":1},{\"instrumentId\":2,\"accompanyingArtistId\":6},{\"instrumentId\":3,\"accompanyingArtistId\":4}]",
"Active": "true11",
"artistData": {}
},
{
"id": 2,
"fileType": "1",
"customFileName": "new file name",
"artists": "[{\"artistId\":1},{\"artistId\":4},{\"artistId\":2},{\"artistId\":6},{\"artistId\":3}]",
"accompanyingArtists": "[{\"instrumentId\":1,\"accompanyingArtistId\":1},{\"instrumentId\":2,\"accompanyingArtistId\":6},{\"instrumentId\":3,\"accompanyingArtistId\":4},{\"instrumentId\":3,\"accompanyingArtistId\":3},{\"instrumentId\":4,\"accompanyingArtistId\":2}]",
"Active": "true11",
"artistData": {}
}
]
}
在控制台页面中,我的艺术家数据属性待定...
enter image description here

最佳答案

customFindAll 函数返回一个 Promise,但 forEach 不会等待它。添加一个 await :

const artistData = await artistService.customfindAll(whereConstraint);
此外, forEach 不会等待,它只是触发异步函数并继续前进。要等待 forEach 完成,您需要一个 map 和一个 Promise.all ,如下所示:
    await Promise.all(data.map(async (element) => {
const artistsArray = [];
let whereConstraint = {};
JSON.parse(element.artists).forEach( async (ele) => {
artistsArray.push(ele.artistId);
});;
whereConstraint = {
id : {
[Op.in]: artistsArray
}
}
element.Active = "true11";
const artistData = await artistService.customfindAll(whereConstraint);
element.artistData = artistData;
console.log("artistData",artistData);
}));
它以这种方式工作的方式是 data.forEach(async (e) => ... 运行异步函数并丢弃结果( promise )。如果您使用 map ,例如 data.map(async (e) => ... ,您将获得一个 Promise 数组,然后使用 await Promise.all(...) 等待它们。

关于asynchronous - 在 foreach 循环中,等待不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63682228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com