如何在循环中链接Angular.JS promise

时间:2021-07-29 11:08:22

I need to design a function in javascript that makes requests to POST multiple items to the database. I then have to take all the data that has been returned and run another $http query to the database.

我需要在javascript中设计一个函数,该函数发出将多个项目POST到数据库的请求。然后我必须获取已返回的所有数据并对数据库运行另一个$ http查询。

This is my code as it stands

这是我的代码

var current_request=null;
var workorders=[];
$scope.submit_work_order=function(){
    for(var i=0;i<$scope.parent_jobs.length;i++){
         addPartAndWorkOrder(equipment_id,part_number,work_order,_id);  
    }

    if(current_request!=null){
        current_request.then(function(){
                    if(workorders.length>0){
                        $http({
                            url:'/api/salesorders', 
                            method: "POST",
                            data: {'workorders':workorders}
                        })
                    }
        });
}

function addPartAndWorkOrder(equipment_id,part_serial_number,work_order_id, job_id){
    //part_serial_number
        var info;

        info=$http({url:'/api/parts', 
                    method: "POST",
                    data: {'equipment':equipment_id});

        if(current_request==null){
            current_request=info;
        }
        else{
            current_request.then(function(){
                return info
                });
            });
        }
        current_request.then(function(data){
            return $http({
                url:'/api/workorders', 
                method: "POST",
                data: {workorder_id:work_order_id, part:data._id,job:job_id}
            }).then(function(data){
                workorders.push(data._id);
            });
        })

}

What I need is to populate the array workorders by running the HTTP requests in the code.

我需要的是通过在代码中运行HTTP请求来填充数组工作器。

Then I need to run the final run another request to the endpoint api/salesorders/ with the data from the array

然后我需要使用数组中的数据运行最终运行端点api / salesorders /的另一个请求

TL;DR

This is the way the endpoints should run

这是端点运行的方式

parts->workorders->parts->workorders->salesorder

This is the way they do run

这是他们运行的方式

parts->salesorder->workorders->parts->workorders

I tried to keep adding to the .then but as it currently stands its still failing

我试图继续添加.then,但目前它仍然失败

1 个解决方案

#1


1  

Break down your calls into dependency tree. It seems that your call pattern must by like so:

将您的调用细分为依赖树。看来你的通话模式必须是这样的:

parts(1) --> workorders(data1) --> _id1
                                        \
parts(2) --> workorders(data2) --> _id2--> salesorder([_id1, id2, _id3])
                                        /
parts(3) --> workorders(data3) --> _id3

So, individually, parts-->workorder must be done sequentially, but all of them can be done in parallel to each other.

因此,单独地,部分 - >工作顺序必须按顺序完成,但所有这些都可以彼此并行完成。

Here's how this can be done (I'm omitting specific API calls and parameters for brevity):

以下是如何完成此操作(为简洁起见,我省略了特定的API调用和参数):

function submitWorkOrder(){
  var parent_jobs = [{}, {}, ]; // this is your array of jobs (however you get it)

  var partsAndOrdersPromises = [];

  parent_jobs.forEach(
    function(job){
       var promise = postPart(job).then(postWorkOrder);
       partsAndOrdersPromises.push(promise);
    });

  var allPartsAndWorkordersPromise = $q.all(partsAndOrdersPromises);

  return allPartsAndWorkordersPromise.then(postSalesOrder);
}

The postSalesOrder will look like so:

postSalesOrder将如下所示:

function postSalesOrder(workorders){
   // workorders is an array of results of each workorder call

   return $http.post("/api/salesorder", ...);
}

OFF-TOPIC: If you have control over your server API, you should consider creating a single API and post all of the info to create parts, workorders, and salesorder at the end as a transaction. It's not a good idea to have an API call for each, and ofcourse, it does not preserve transactional integrity (e.g. /api/salesorder might fail, but you have already submitted the previous orders)

OFF-TOPIC:如果您可以控制服务器API,则应考虑创建单个API并将所有信息发布到最后作为事务创建零件,工作单和销售订单。为每个API调用并不是一个好主意,当然,它不会保留事务完整性(例如/ api / salesorder可能会失败,但您已经提交了以前的订单)

#1


1  

Break down your calls into dependency tree. It seems that your call pattern must by like so:

将您的调用细分为依赖树。看来你的通话模式必须是这样的:

parts(1) --> workorders(data1) --> _id1
                                        \
parts(2) --> workorders(data2) --> _id2--> salesorder([_id1, id2, _id3])
                                        /
parts(3) --> workorders(data3) --> _id3

So, individually, parts-->workorder must be done sequentially, but all of them can be done in parallel to each other.

因此,单独地,部分 - >工作顺序必须按顺序完成,但所有这些都可以彼此并行完成。

Here's how this can be done (I'm omitting specific API calls and parameters for brevity):

以下是如何完成此操作(为简洁起见,我省略了特定的API调用和参数):

function submitWorkOrder(){
  var parent_jobs = [{}, {}, ]; // this is your array of jobs (however you get it)

  var partsAndOrdersPromises = [];

  parent_jobs.forEach(
    function(job){
       var promise = postPart(job).then(postWorkOrder);
       partsAndOrdersPromises.push(promise);
    });

  var allPartsAndWorkordersPromise = $q.all(partsAndOrdersPromises);

  return allPartsAndWorkordersPromise.then(postSalesOrder);
}

The postSalesOrder will look like so:

postSalesOrder将如下所示:

function postSalesOrder(workorders){
   // workorders is an array of results of each workorder call

   return $http.post("/api/salesorder", ...);
}

OFF-TOPIC: If you have control over your server API, you should consider creating a single API and post all of the info to create parts, workorders, and salesorder at the end as a transaction. It's not a good idea to have an API call for each, and ofcourse, it does not preserve transactional integrity (e.g. /api/salesorder might fail, but you have already submitted the previous orders)

OFF-TOPIC:如果您可以控制服务器API,则应考虑创建单个API并将所有信息发布到最后作为事务创建零件,工作单和销售订单。为每个API调用并不是一个好主意,当然,它不会保留事务完整性(例如/ api / salesorder可能会失败,但您已经提交了以前的订单)