最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用异步等待构建api

网站源码admin23浏览0评论

使用异步等待构建api

使用异步等待构建api

我已经用express编写了一个Node api。在此api中,我将从数据库处理中获取一些数据,并将最终数据作为api负载发送。

这是我的代码。

exports.getAllAco =  (req, res) => {
    let acoList = [], i = 0;

    let condaions = {
        deleted: 0,
    }

    let attributes = ['id', 'name', 'email', 'type', 'phone', 'gender', 'main_sys_id', 'avatar', 'in_service', 'created_by']
    Methods.getAllData(User, condaions, attributes).then((userList) => {
            userList.map( user => {
                i = i + 1;
                if (user.dataValues.type == 'aco') {
                    let condations = {
                        aquisition_member_id: user.dataValues.id,
                        deleted: 0
                    }

                    Methods.getDetailsFromTwoAssociateTable(condations, Task, DetailTask).then(tasks => {
                        let x = {
                            user: user,
                            tasks: tasks
                        }

                        x.user.dataValues.totalTask = tasks.length;

                        let index = 0;

                        x.user.dataValues.userTotalAssigned = 0, x.user.dataValues.userTotalCalled = 0, x.user.dataValues.userTotalConverted = 0, x.user.dataValues.userTotalRejected = 0;

                        tasks.map(v=>{
                            x.tasks[index].dataValues.totalAssignedCustomer = v.dataValues.detail_tasks.length;
                            x.tasks[index].dataValues.totalCalled = 0;
                            x.tasks[index].dataValues.totalConverted = 0;
                            x.tasks[index].dataValues.totalRejected = 0;
                            v.dataValues.detail_tasks.map(detail_task=>{

                                 x.user.dataValues.userTotalAssigned += 1;

                                if(detail_task.dataValues.phone_call_status == 'called' ){
                                    x.tasks[index].dataValues.totalCalled += 1;
                                    x.user.dataValues.userTotalCalled +=1;
                                }

                                else if(detail_task.dataValues.phone_call_status == 'confirmed'){
                                    x.tasks[index].dataValues.totalConverted +=1;
                                    x.user.dataValues.userTotalConverted += 1;
                                }

                                else{
                                    x.user.dataValues.userTotalRejected += 1;
                                    x.tasks[index].dataValues.totalRejected += 1;
                                }
                            })
                            index=index+1;
                        })

                        // x.user.dataValues.totalAssigned = totalAssigned;

                        acoList.push(x)                           
                    })
                }
            })
            return acoList;
    }).then(acoList=>{
       setTimeout(() => {
        Methods.successResponse(req, res, acoList)
       }, 2000);
    }).catch((error) => {
        ErrorResMethods.errorResponse(req, res, error);
    })
}

要发送实际结果,我必须等待两秒钟,否则响应为空。

我如何通过异步/等待来实现。

回答如下:

我想类似的事情应该可以完成。

exports.getAllAco = async (req, res) => {
  let i = 0;

  const condaions = {
    "deleted": 0
  };

  const attributes = ["id", "name", "email", "type", "phone", "gender", "main_sys_id", "avatar", "in_service", "created_by"];
  const userList = await Methods.getAllData(User, condaions, attributes);
  const acoList = await Promise.all(userList.map(async user => {
    i += 1;
    if (user.dataValues.type == "aco") {
      const condations = {
        "aquisition_member_id": user.dataValues.id,
        "deleted": 0
      };

      const tasks = await Methods.getDetailsFromTwoAssociateTable(condations, Task, DetailTask);
      const x = {
        "user": user,
        "tasks": tasks
      };

      x.user.dataValues.totalTask = tasks.length;

      let index = 0;

      x.user.dataValues.userTotalAssigned = 0, x.user.dataValues.userTotalCalled = 0, x.user.dataValues.userTotalConverted = 0, x.user.dataValues.userTotalRejected = 0;

      tasks.forEach(v => {
        x.tasks[index].dataValues.totalAssignedCustomer = v.dataValues.detail_tasks.length;
        x.tasks[index].dataValues.totalCalled = 0;
        x.tasks[index].dataValues.totalConverted = 0;
        x.tasks[index].dataValues.totalRejected = 0;
        v.dataValues.detail_tasks.map(detail_task => {

          x.user.dataValues.userTotalAssigned += 1;

          if (detail_task.dataValues.phone_call_status == "called") {
            x.tasks[index].dataValues.totalCalled += 1;
            x.user.dataValues.userTotalCalled += 1;
          } else if (detail_task.dataValues.phone_call_status == "confirmed") {
            x.tasks[index].dataValues.totalConverted += 1;
            x.user.dataValues.userTotalConverted += 1;
          } else {
            x.user.dataValues.userTotalRejected += 1;
            x.tasks[index].dataValues.totalRejected += 1;
          }
        });
        index += 1;
      });
      return x;
    }
  })
  setTimeout(() => {
    Methods.successResponse(req, res, acoList);
  }, 2000));
};

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论