Javascript异步功能流程 [英] Javascript async function flow

查看:76
本文介绍了Javascript异步功能流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有座位,我的职能部门应该在座位上分配一名员工.我不明白为什么即使我使用"await",该程序也不能起到同步的作用.

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".

在函数的第一行中,程序按预期方式运行.它等待从数据库中获取"seats",然后执行"if(seats.length> 0)" 检查并初始化一个空数组.

In the first lines of the function, the program acts as expected. it waits to get "seats"from the database, then performs the "if(seats.length > 0)" check and initialises an empty array.

async function AssignSeat(req, res) {

  var seats = await connection.SeatEmployees.findAll({
    where: {
      SeatId: req.body.seat.SeatId
    }
  })
  .catch(err => {
    res.status(err.status).json(err)
  });


  if(seats.length > 0){
    var isShared = true;

    var employees = [];

    await seats.forEach(async function(seat){
      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)
    });


    employees.forEach(employee => {
      if(employee.frequent == true)
          isShared = false;
    })


    if(isShared == true){
      //assign user to seat;
    }
  }
}

我的问题是在代码的第13行,即"await seat.forEach(async function(seat)").

My problem is at the 13th line of code, at " await seats.forEach(async function(seat)".

我想做的是遍历座位"的每个元素,获取分配给该座位的员工,然后将其推入员工"数组.

What I want to do is go through each element of "seats", Get the employee assigned to that seat, and push it into the "employees" array.

仅在从所有座位进行迭代并填充完雇员数组之后,我才要继续执行"employees.forEach(employee => {"行.)

Only after iterating from all the seats and filling the employees array, I want proceed with the "employees.forEach(employee => {" line.

相反,发生的是在调用
之后 -----"var employee = await connection.EmployeesGraph.findAll({" ----,程序不等待序列化从数据库中获取雇员,然后转到----"employees.push (员工);"----按预期.
到达----" employees.push(employee);之后的行上的括号,然后出现错误"TypeError:无法读取未定义的属性'catch'".

Instead, what happens is that after calling
-----"var employee = await connection.EmployeesGraph.findAll({ "---- ,the program doesn't wait for sequelize to get the employee from the database and then go to ----"employees.push(employee);"---- , as intended.
It goes to the paranthesis on the line after ----"employees.push(employee);"---- , then I get the error "TypeError: Cannot read property 'catch' of undefined".

您能解释一下为什么会发生这种情况吗?

Could you please explain why this happens?

推荐答案

最简单的解决方案是使用实际的for循环代替forEach来完成此任务. forEach()等不及要遍历所有内容.

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

这篇关于Javascript异步功能流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆