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

Asynchronous 在foreach循环中,等待不起作用,asynchronous,foreach,async-await,sequelize.js,Asynchronous,Foreach,Async Await,Sequelize.js,下面是代码downloadcontroller.js // Retrieve all downloads from the database. exports.findAll = async (req, res) => { downloadObj .findAll( { include: [ { model: composerObj, required: false, },

下面是代码downloadcontroller.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 artistData=artistService.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": {}
        }
    ]
}
在控制台页面中,I artistdata属性挂起。。。
函数
customFindAll
返回一个承诺,但
forEach
并不等待它。添加一个
等待

const artistData = await artistService.customfindAll(whereConstraint);
而且,
forEach
并不等待,它只是触发异步函数并继续。要等待
forEach
完成,您需要一个
map
和一个
Promise。所有
,如下所示:

    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)=>…
运行异步函数并丢弃结果(承诺)。如果您使用
map
,如
data.map(async(e)=>…
,您将得到一个承诺数组,然后使用
等待承诺。all(…)
等待它们。

函数
customFindAll
返回承诺,但
forEach
不等待。添加一个
wait

const artistData = await artistService.customfindAll(whereConstraint);
另外,
forEach
并不等待,它只是启动异步函数并继续。要等待
forEach
完成,您需要一个
map
和一个
Promise。所有
,如下所示:

    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)=>…
运行异步函数并丢弃结果(承诺)。如果您使用
map
,如
data.map(async(e)=>…
),您将获得一个承诺数组,然后使用
等待承诺。所有(…)
等待它们