• <tfoot id='caAsc'></tfoot>
    • <bdo id='caAsc'></bdo><ul id='caAsc'></ul>
    <i id='caAsc'><tr id='caAsc'><dt id='caAsc'><q id='caAsc'><span id='caAsc'><b id='caAsc'><form id='caAsc'><ins id='caAsc'></ins><ul id='caAsc'></ul><sub id='caAsc'></sub></form><legend id='caAsc'></legend><bdo id='caAsc'><pre id='caAsc'><center id='caAsc'></center></pre></bdo></b><th id='caAsc'></th></span></q></dt></tr></i><div id='caAsc'><tfoot id='caAsc'></tfoot><dl id='caAsc'><fieldset id='caAsc'></fieldset></dl></div>

        <small id='caAsc'></small><noframes id='caAsc'>

        <legend id='caAsc'><style id='caAsc'><dir id='caAsc'><q id='caAsc'></q></dir></style></legend>
      1. Javascript异步函数流程

        Javascript async function flow(Javascript异步函数流程)
        • <small id='Djvvs'></small><noframes id='Djvvs'>

          <tfoot id='Djvvs'></tfoot>
          1. <legend id='Djvvs'><style id='Djvvs'><dir id='Djvvs'><q id='Djvvs'></q></dir></style></legend>
              <bdo id='Djvvs'></bdo><ul id='Djvvs'></ul>
            • <i id='Djvvs'><tr id='Djvvs'><dt id='Djvvs'><q id='Djvvs'><span id='Djvvs'><b id='Djvvs'><form id='Djvvs'><ins id='Djvvs'></ins><ul id='Djvvs'></ul><sub id='Djvvs'></sub></form><legend id='Djvvs'></legend><bdo id='Djvvs'><pre id='Djvvs'><center id='Djvvs'></center></pre></bdo></b><th id='Djvvs'></th></span></q></dt></tr></i><div id='Djvvs'><tfoot id='Djvvs'></tfoot><dl id='Djvvs'><fieldset id='Djvvs'></fieldset></dl></div>
                  <tbody id='Djvvs'></tbody>
                1. 本文介绍了Javascript异步函数流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果有空位,我的职能部门应该分配一名员工.我不明白为什么即使我使用了等待",程序也不能同步.

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

                  在函数的第一行,程序按预期运行.它等待从数据库中获取席位",然后执行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({ "---- ,程序不等待sequelize从数据库中获取员工然后去----"employees.push(employee);"---- ,如预期的那样.
                  它转到 ----"employees.push(employee);"---- 之后的行上的括号,然后我收到错误 "TypeError: Cannot read property 'catch' of undefined".

                  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异步函数流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

                  <tfoot id='4ARLr'></tfoot>

                  <legend id='4ARLr'><style id='4ARLr'><dir id='4ARLr'><q id='4ARLr'></q></dir></style></legend>

                    <tbody id='4ARLr'></tbody>
                      • <i id='4ARLr'><tr id='4ARLr'><dt id='4ARLr'><q id='4ARLr'><span id='4ARLr'><b id='4ARLr'><form id='4ARLr'><ins id='4ARLr'></ins><ul id='4ARLr'></ul><sub id='4ARLr'></sub></form><legend id='4ARLr'></legend><bdo id='4ARLr'><pre id='4ARLr'><center id='4ARLr'></center></pre></bdo></b><th id='4ARLr'></th></span></q></dt></tr></i><div id='4ARLr'><tfoot id='4ARLr'></tfoot><dl id='4ARLr'><fieldset id='4ARLr'></fieldset></dl></div>
                          <bdo id='4ARLr'></bdo><ul id='4ARLr'></ul>
                        • <small id='4ARLr'></small><noframes id='4ARLr'>