Skip to content Skip to sidebar Skip to footer

Javascript Async Function Flow

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'. In the first li

Solution 1:

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)
}

Post a Comment for "Javascript Async Function Flow"