Javascript async function flow

前端 未结 1 1473
谎友^
谎友^ 2021-01-26 11:05

My function should assign an employee on a seat if the seat is available. I do not understand why the program doesn\'t act as synchronous even though I used \"await\".

I

相关标签:
1条回答
  • 2021-01-26 11:34

    The easiest solution is to use an actual for loop instead of forEach for this task. forEach() won't wait to iterate over everything.

    try {
      for (const seat of seats) {
        var employee = await connection.EmployeesGraph.findAll({
            where: {
              id: seat.EmployeeGraphId
            }
          })
          .catch(err => {
            res.status(err.status).json(err)
          });
    
        employees.push(employee);
      }
    } catch (err) {
      res.status(err.status).json(err)
    }

    0 讨论(0)
提交回复
热议问题