异步/等待循环嵌套

时间:2018-10-16 15:21:05

标签: javascript node.js async-await

我有一个基本的快速路由器。有一个对数据库的异步调用以获取数据数组。然后,使用此数组,我将循环执行另一个异步调用。但是我不知道如何使它按我想要的顺序工作。 代码:

const db = require('../models/index');
const router = require('express').Router();
const Op = db.Sequelize.Op;

router.get('/', async function (req, res) {
  const dataSet = await db.sequelize.models.model1.findAll({
    raw: true,
    include: [{
      model: db.sequelize.models.model2,
    }, {
      model: db.sequelize.models.model3,
      required: true
    }],
    limit: 10,
    order: ['flight_date']
  });

  dataSet.forEach(async (item) => {
    delete item.id;
    const mealQtyP = await db.sequelize.models.model4.findAll({
      raw: true,
      where: {
        sampleField: sampleFieldCondition,
      }
    });

    console.log('from cycle'); //but it logged after "shall log after all"
  });

  console.log('shall log after all'); //want it to be logged after all
});

module.exports = router;

1 个答案:

答案 0 :(得分:1)

如果要在处理dataSet中的每个项目之后 打印“毕竟要保留日志”,可以将数据集项目映射到Promise,然后然后在awaitPromise.all()

await Promise.all(dataSet.map(async (item) => {
    delete item.id;
    const mealQtyP = await db.sequelize.models.model4.findAll({
        raw: true,
        where: {
            sampleField: sampleFieldCondition,
        }
    });

    console.log('from cycle'); //but it logged after "shall log after all"
}));

console.log('shall log after all'); //want it to be logged after all

由于您要将异步函数传递给map,因此它将返回Promise,因此map的结果是Promise的数组。 Promise.all()返回一个Promise,当数组中所有原始Promise都被解析时,解析的结果将被解析;因此,等待此Promise将等待,直到dataSet中的每个项目都已处理完毕。

修改此代码以实际为您提供结果:

const results = await Promise.all(dataSet.map(async (item) => {
    console.log('from cycle'); //but it logged after "shall log after all"
    delete item.id;
    return db.sequelize.models.model4.findAll({
        raw: true,
        where: {
            sampleField: sampleFieldCondition,
        }
    });
}));

console.log('shall log after all'); //want it to be logged after all
console.log('here are my results:', results);